commits_spec.rb 42.2 KB
Newer Older
1 2 3
require 'spec_helper'
require 'mime/types'

4
describe API::Commits do
5
  let(:user) { create(:user) }
6 7 8 9 10 11 12
  let(:guest) { create(:user).tap { |u| project.add_guest(u) } }
  let(:project) { create(:project, :repository, creator: user, path: 'my.project') }
  let(:branch_with_dot) { project.repository.find_branch('ends-with.json') }
  let(:branch_with_slash) { project.repository.find_branch('improve/awesome') }

  let(:project_id) { project.id }
  let(:current_user) { nil }
13

14
  before do
15
    project.add_maintainer(user)
16
  end
17

18
  describe 'GET /projects/:id/repository/commits' do
19 20
    let(:route) { "/projects/#{project_id}/repository/commits" }

21
    shared_examples_for 'project commits' do |schema: 'public_api/v4/commits'|
22
      it "returns project commits" do
23
        commit = project.repository.commit
24

25
        get api(route, current_user)
26

27
        expect(response).to have_gitlab_http_status(200)
28
        expect(response).to match_response_schema(schema)
29 30 31
        expect(json_response.first['id']).to eq(commit.id)
        expect(json_response.first['committer_name']).to eq(commit.committer_name)
        expect(json_response.first['committer_email']).to eq(commit.committer_email)
32
      end
33 34 35 36

      it 'include correct pagination headers' do
        commit_count = project.repository.count_commits(ref: 'master').to_s

37
        get api(route, current_user)
38 39 40 41 42

        expect(response).to include_pagination_headers
        expect(response.headers['X-Total']).to eq(commit_count)
        expect(response.headers['X-Page']).to eql('1')
      end
43 44
    end

45 46 47 48 49
    context 'when unauthenticated', 'and project is public' do
      let(:project) { create(:project, :public, :repository) }

      it_behaves_like 'project commits'
    end
50

51 52 53 54
    context 'when unauthenticated', 'and project is private' do
      it_behaves_like '404 response' do
        let(:request) { get api(route) }
        let(:message) { '404 Project Not Found' }
55 56
      end
    end
57

58
    context 'when authenticated', 'as a maintainer' do
59
      let(:current_user) { user }
60

61
      it_behaves_like 'project commits'
62

63 64
      context "since optional parameter" do
        it "returns project commits since provided parameter" do
65
          commits = project.repository.commits("master", limit: 2)
66
          after = commits.second.created_at
67

68
          get api("/projects/#{project_id}/repository/commits?since=#{after.utc.iso8601}", user)
69

70 71 72 73
          expect(json_response.size).to eq 2
          expect(json_response.first["id"]).to eq(commits.first.id)
          expect(json_response.second["id"]).to eq(commits.second.id)
        end
74

75
        it 'include correct pagination headers' do
76
          commits = project.repository.commits("master", limit: 2)
77 78 79 80 81 82 83 84 85
          after = commits.second.created_at
          commit_count = project.repository.count_commits(ref: 'master', after: after).to_s

          get api("/projects/#{project_id}/repository/commits?since=#{after.utc.iso8601}", user)

          expect(response).to include_pagination_headers
          expect(response.headers['X-Total']).to eq(commit_count)
          expect(response.headers['X-Page']).to eql('1')
        end
86
      end
87

88 89
      context "until optional parameter" do
        it "returns project commits until provided parameter" do
90
          commits = project.repository.commits("master", limit: 20)
91
          before = commits.second.created_at
92

93
          get api("/projects/#{project_id}/repository/commits?until=#{before.utc.iso8601}", user)
94

95
          if commits.size == 20
96 97 98 99
            expect(json_response.size).to eq(20)
          else
            expect(json_response.size).to eq(commits.size - 1)
          end
100

101 102 103
          expect(json_response.first["id"]).to eq(commits.second.id)
          expect(json_response.second["id"]).to eq(commits.third.id)
        end
104

105
        it 'include correct pagination headers' do
106
          commits = project.repository.commits("master", limit: 2)
107 108
          before = commits.second.created_at
          commit_count = project.repository.count_commits(ref: 'master', before: before).to_s
109

110
          get api("/projects/#{project_id}/repository/commits?until=#{before.utc.iso8601}", user)
111

112 113 114 115
          expect(response).to include_pagination_headers
          expect(response.headers['X-Total']).to eq(commit_count)
          expect(response.headers['X-Page']).to eql('1')
        end
116
      end
117

118 119 120
      context "invalid xmlschema date parameters" do
        it "returns an invalid parameter error message" do
          get api("/projects/#{project_id}/repository/commits?since=invalid-date", user)
121

122
          expect(response).to have_gitlab_http_status(400)
123 124
          expect(json_response['error']).to eq('since is invalid')
        end
125
      end
126

127 128 129 130
      context "path optional parameter" do
        it "returns project commits matching provided path parameter" do
          path = 'files/ruby/popen.rb'
          commit_count = project.repository.count_commits(ref: 'master', path: path).to_s
131

132
          get api("/projects/#{project_id}/repository/commits?path=#{path}", user)
133

134 135 136 137 138
          expect(json_response.size).to eq(3)
          expect(json_response.first["id"]).to eq("570e7b2abdd848b95f2f578043fc23bd6f6fd24d")
          expect(response).to include_pagination_headers
          expect(response.headers['X-Total']).to eq(commit_count)
        end
139

140 141 142
        it 'include correct pagination headers' do
          path = 'files/ruby/popen.rb'
          commit_count = project.repository.count_commits(ref: 'master', path: path).to_s
143

144
          get api("/projects/#{project_id}/repository/commits?path=#{path}", user)
145

146 147 148 149
          expect(response).to include_pagination_headers
          expect(response.headers['X-Total']).to eq(commit_count)
          expect(response.headers['X-Page']).to eql('1')
        end
150
      end
151

Tiago Botelho's avatar
Tiago Botelho committed
152 153 154 155 156 157 158 159 160 161 162 163
      context 'all optional parameter' do
        it 'returns all project commits' do
          commit_count = project.repository.count_commits(all: true)

          get api("/projects/#{project_id}/repository/commits?all=true", user)

          expect(response).to include_pagination_headers
          expect(response.headers['X-Total']).to eq(commit_count.to_s)
          expect(response.headers['X-Page']).to eql('1')
        end
      end

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
      context 'with_stats optional parameter' do
        let(:project) { create(:project, :public, :repository) }

        it_behaves_like 'project commits', schema: 'public_api/v4/commits_with_stats' do
          let(:route) { "/projects/#{project_id}/repository/commits?with_stats=true" }

          it 'include commits details' do
            commit = project.repository.commit
            get api(route, current_user)

            expect(json_response.first['stats']['additions']).to eq(commit.stats.additions)
            expect(json_response.first['stats']['deletions']).to eq(commit.stats.deletions)
            expect(json_response.first['stats']['total']).to eq(commit.stats.total)
          end
        end
      end

181 182 183 184 185 186 187
      context 'with pagination params' do
        let(:page) { 1 }
        let(:per_page) { 5 }
        let(:ref_name) { 'master' }
        let!(:request) do
          get api("/projects/#{project_id}/repository/commits?page=#{page}&per_page=#{per_page}&ref_name=#{ref_name}", user)
        end
188

189 190
        it 'returns correct headers' do
          commit_count = project.repository.count_commits(ref: ref_name).to_s
191

192 193 194 195 196 197
          expect(response).to include_pagination_headers
          expect(response.headers['X-Total']).to eq(commit_count)
          expect(response.headers['X-Page']).to eq('1')
          expect(response.headers['Link']).to match(/page=1&per_page=5/)
          expect(response.headers['Link']).to match(/page=2&per_page=5/)
        end
198

199 200 201
        context 'viewing the first page' do
          it 'returns the first 5 commits' do
            commit = project.repository.commit
202

203 204 205 206
            expect(json_response.size).to eq(per_page)
            expect(json_response.first['id']).to eq(commit.id)
            expect(response.headers['X-Page']).to eq('1')
          end
207 208
        end

209 210
        context 'viewing the third page' do
          let(:page) { 3 }
211

212
          it 'returns the third 5 commits' do
213
            commit = project.repository.commits('HEAD', limit: per_page, offset: (page - 1) * per_page).first
214

215 216 217 218
            expect(json_response.size).to eq(per_page)
            expect(json_response.first['id']).to eq(commit.id)
            expect(response.headers['X-Page']).to eq('3')
          end
219 220 221
        end
      end
    end
222 223
  end

224
  describe "POST /projects/:id/repository/commits" do
225
    let!(:url) { "/projects/#{project_id}/repository/commits" }
Marc Siegfriedt's avatar
Marc Siegfriedt committed
226 227

    it 'returns a 403 unauthorized for user without permissions' do
228
      post api(url, guest)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
229

230
      expect(response).to have_gitlab_http_status(403)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
231 232 233 234 235
    end

    it 'returns a 400 bad request if no params are given' do
      post api(url, user)

236
      expect(response).to have_gitlab_http_status(400)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
237 238
    end

239
    describe 'create' do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
240
      let(:message) { 'Created file' }
241
      let(:invalid_c_params) do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
242
        {
243
          branch: 'master',
Marc Siegfriedt's avatar
Marc Siegfriedt committed
244 245 246 247 248 249 250 251 252 253
          commit_message: message,
          actions: [
            {
              action: 'create',
              file_path: 'files/ruby/popen.rb',
              content: 'puts 8'
            }
          ]
        }
      end
254
      let(:valid_c_params) do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
255
        {
256
          branch: 'master',
Marc Siegfriedt's avatar
Marc Siegfriedt committed
257 258 259 260 261 262 263 264 265 266
          commit_message: message,
          actions: [
            {
              action: 'create',
              file_path: 'foo/bar/baz.txt',
              content: 'puts 8'
            }
          ]
        }
      end
267
      let(:valid_utf8_c_params) do
268 269 270 271 272 273 274 275 276 277 278 279
        {
          branch: 'master',
          commit_message: message,
          actions: [
            {
              action: 'create',
              file_path: 'foo/bar/baz.txt',
              content: 'puts 🦊'
            }
          ]
        }
      end
Marc Siegfriedt's avatar
Marc Siegfriedt committed
280 281 282 283

      it 'a new file in project repo' do
        post api(url, user), valid_c_params

284
        expect(response).to have_gitlab_http_status(201)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
285
        expect(json_response['title']).to eq(message)
286 287
        expect(json_response['committer_name']).to eq(user.name)
        expect(json_response['committer_email']).to eq(user.email)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
288 289
      end

290 291 292 293 294 295 296 297 298
      it 'a new file with utf8 chars in project repo' do
        post api(url, user), valid_utf8_c_params

        expect(response).to have_gitlab_http_status(201)
        expect(json_response['title']).to eq(message)
        expect(json_response['committer_name']).to eq(user.name)
        expect(json_response['committer_email']).to eq(user.email)
      end

Marc Siegfriedt's avatar
Marc Siegfriedt committed
299 300 301
      it 'returns a 400 bad request if file exists' do
        post api(url, user), invalid_c_params

302
        expect(response).to have_gitlab_http_status(400)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
303
      end
304

305 306
      context 'with project path containing a dot in URL' do
        let(:url) { "/projects/#{CGI.escape(project.full_path)}/repository/commits" }
307 308 309 310

        it 'a new file in project repo' do
          post api(url, user), valid_c_params

311
          expect(response).to have_gitlab_http_status(201)
312 313
        end
      end
Marc Siegfriedt's avatar
Marc Siegfriedt committed
314 315
    end

316
    describe 'delete' do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
317
      let(:message) { 'Deleted file' }
318
      let(:invalid_d_params) do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
319
        {
320
          branch: 'markdown',
Marc Siegfriedt's avatar
Marc Siegfriedt committed
321 322 323 324 325 326 327 328 329
          commit_message: message,
          actions: [
            {
              action: 'delete',
              file_path: 'doc/api/projects.md'
            }
          ]
        }
      end
330
      let(:valid_d_params) do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
331
        {
332
          branch: 'markdown',
Marc Siegfriedt's avatar
Marc Siegfriedt committed
333 334 335 336 337 338 339 340 341 342 343 344 345
          commit_message: message,
          actions: [
            {
              action: 'delete',
              file_path: 'doc/api/users.md'
            }
          ]
        }
      end

      it 'an existing file in project repo' do
        post api(url, user), valid_d_params

346
        expect(response).to have_gitlab_http_status(201)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
347 348 349 350 351 352
        expect(json_response['title']).to eq(message)
      end

      it 'returns a 400 bad request if file does not exist' do
        post api(url, user), invalid_d_params

353
        expect(response).to have_gitlab_http_status(400)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
354 355 356
      end
    end

357
    describe 'move' do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
358
      let(:message) { 'Moved file' }
359
      let(:invalid_m_params) do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
360
        {
361
          branch: 'feature',
Marc Siegfriedt's avatar
Marc Siegfriedt committed
362 363 364 365 366 367 368 369 370 371 372
          commit_message: message,
          actions: [
            {
              action: 'move',
              file_path: 'CHANGELOG',
              previous_path: 'VERSION',
              content: '6.7.0.pre'
            }
          ]
        }
      end
373
      let(:valid_m_params) do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
374
        {
375
          branch: 'feature',
Marc Siegfriedt's avatar
Marc Siegfriedt committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
          commit_message: message,
          actions: [
            {
              action: 'move',
              file_path: 'VERSION.txt',
              previous_path: 'VERSION',
              content: '6.7.0.pre'
            }
          ]
        }
      end

      it 'an existing file in project repo' do
        post api(url, user), valid_m_params

391
        expect(response).to have_gitlab_http_status(201)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
392 393 394 395 396 397
        expect(json_response['title']).to eq(message)
      end

      it 'returns a 400 bad request if file does not exist' do
        post api(url, user), invalid_m_params

398
        expect(response).to have_gitlab_http_status(400)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
399 400 401
      end
    end

402
    describe 'update' do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
403
      let(:message) { 'Updated file' }
404
      let(:invalid_u_params) do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
405
        {
406
          branch: 'master',
Marc Siegfriedt's avatar
Marc Siegfriedt committed
407 408 409 410 411 412 413 414 415 416
          commit_message: message,
          actions: [
            {
              action: 'update',
              file_path: 'foo/bar.baz',
              content: 'puts 8'
            }
          ]
        }
      end
417
      let(:valid_u_params) do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
418
        {
419
          branch: 'master',
Marc Siegfriedt's avatar
Marc Siegfriedt committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433
          commit_message: message,
          actions: [
            {
              action: 'update',
              file_path: 'files/ruby/popen.rb',
              content: 'puts 8'
            }
          ]
        }
      end

      it 'an existing file in project repo' do
        post api(url, user), valid_u_params

434
        expect(response).to have_gitlab_http_status(201)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
435 436 437 438 439 440
        expect(json_response['title']).to eq(message)
      end

      it 'returns a 400 bad request if file does not exist' do
        post api(url, user), invalid_u_params

441
        expect(response).to have_gitlab_http_status(400)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
442 443 444
      end
    end

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    describe 'chmod' do
      let(:message) { 'Chmod +x file' }
      let(:file_path) { 'files/ruby/popen.rb' }
      let(:execute_filemode) { true }
      let(:params) do
        {
          branch: 'master',
          commit_message: message,
          actions: [
            {
              action: 'chmod',
              file_path: file_path,
              execute_filemode: execute_filemode
            }
          ]
        }
      end

      it 'responds with success' do
        post api(url, user), params

        expect(response).to have_gitlab_http_status(201)
        expect(json_response['title']).to eq(message)
      end

      context 'when execute_filemode is false' do
        let(:execute_filemode) { false }

        it 'responds with success' do
          post api(url, user), params

          expect(response).to have_gitlab_http_status(201)
          expect(json_response['title']).to eq(message)
        end
      end

      context "when the file doesn't exists" do
        let(:file_path) { 'foo/bar.baz' }

        it "responds with 400" do
          post api(url, user), params

          expect(response).to have_gitlab_http_status(400)
          expect(json_response['message']).to eq("A file with this name doesn't exist")
        end
      end
    end

493
    describe 'multiple operations' do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
494
      let(:message) { 'Multiple actions' }
495
      let(:invalid_mo_params) do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
496
        {
497
          branch: 'master',
Marc Siegfriedt's avatar
Marc Siegfriedt committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
          commit_message: message,
          actions: [
            {
              action: 'create',
              file_path: 'files/ruby/popen.rb',
              content: 'puts 8'
            },
            {
              action: 'delete',
              file_path: 'doc/api/projects.md'
            },
            {
              action: 'move',
              file_path: 'CHANGELOG',
              previous_path: 'VERSION',
              content: '6.7.0.pre'
            },
            {
              action: 'update',
              file_path: 'foo/bar.baz',
              content: 'puts 8'
519 520 521 522 523
            },
            {
              action: 'chmod',
              file_path: 'files/ruby/popen.rb',
              execute_filemode: true
Marc Siegfriedt's avatar
Marc Siegfriedt committed
524 525 526 527
            }
          ]
        }
      end
528
      let(:valid_mo_params) do
Marc Siegfriedt's avatar
Marc Siegfriedt committed
529
        {
530
          branch: 'master',
Marc Siegfriedt's avatar
Marc Siegfriedt committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
          commit_message: message,
          actions: [
            {
              action: 'create',
              file_path: 'foo/bar/baz.txt',
              content: 'puts 8'
            },
            {
              action: 'delete',
              file_path: 'Gemfile.zip'
            },
            {
              action: 'move',
              file_path: 'VERSION.txt',
              previous_path: 'VERSION',
              content: '6.7.0.pre'
            },
            {
              action: 'update',
              file_path: 'files/ruby/popen.rb',
              content: 'puts 8'
552 553 554 555 556
            },
            {
              action: 'chmod',
              file_path: 'files/ruby/popen.rb',
              execute_filemode: true
Marc Siegfriedt's avatar
Marc Siegfriedt committed
557 558 559 560 561 562 563 564
            }
          ]
        }
      end

      it 'are commited as one in project repo' do
        post api(url, user), valid_mo_params

565
        expect(response).to have_gitlab_http_status(201)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
566 567 568 569 570 571
        expect(json_response['title']).to eq(message)
      end

      it 'return a 400 bad request if there are any issues' do
        post api(url, user), invalid_mo_params

572
        expect(response).to have_gitlab_http_status(400)
Marc Siegfriedt's avatar
Marc Siegfriedt committed
573 574
      end
    end
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606

    context 'when committing into a fork as a maintainer' do
      include_context 'merge request allowing collaboration'

      let(:project_id) { forked_project.id }

      def push_params(branch_name)
        {
          branch: branch_name,
          commit_message: 'Hello world',
          actions: [
            {
              action: 'create',
              file_path: 'foo/bar/baz.txt',
              content: 'puts 8'
            }
          ]
        }
      end

      it 'allows pushing to the source branch of the merge request' do
        post api(url, user), push_params('feature')

        expect(response).to have_gitlab_http_status(:created)
      end

      it 'denies pushing to another branch' do
        post api(url, user), push_params('other-branch')

        expect(response).to have_gitlab_http_status(:forbidden)
      end
    end
Marc Siegfriedt's avatar
Marc Siegfriedt committed
607 608
  end

Robert Schilling's avatar
Robert Schilling committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
  describe 'GET /projects/:id/repository/commits/:sha/refs' do
    let(:project) { create(:project, :public, :repository) }
    let(:tag) { project.repository.find_tag('v1.1.0') }
    let(:commit_id) { tag.dereferenced_target.id }
    let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}/refs" }

    context 'when ref does not exist' do
      let(:commit_id) { 'unknown' }

      it_behaves_like '404 response' do
        let(:request) { get api(route, current_user) }
        let(:message) { '404 Commit Not Found' }
      end
    end

    context 'when repository is disabled' do
      include_context 'disabled repository'

      it_behaves_like '403 response' do
        let(:request) { get api(route, current_user) }
      end
    end

    context 'for a valid commit' do
      it 'returns all refs with no scope' do
634
        get api(route, current_user), per_page: 100
Robert Schilling's avatar
Robert Schilling committed
635

636 637
        refs = project.repository.branch_names_contains(commit_id).map {|name| ['branch', name]}
        refs.concat(project.repository.tag_names_contains(commit_id).map {|name| ['tag', name]})
Robert Schilling's avatar
Robert Schilling committed
638

639 640 641 642
        expect(response).to have_gitlab_http_status(200)
        expect(response).to include_pagination_headers
        expect(json_response).to be_an Array
        expect(json_response.map { |r| [r['type'], r['name']] }.compact).to eq(refs)
Robert Schilling's avatar
Robert Schilling committed
643 644 645
      end

      it 'returns all refs' do
646
        get api(route, current_user), type: 'all', per_page: 100
Robert Schilling's avatar
Robert Schilling committed
647

648 649
        refs = project.repository.branch_names_contains(commit_id).map {|name| ['branch', name]}
        refs.concat(project.repository.tag_names_contains(commit_id).map {|name| ['tag', name]})
Robert Schilling's avatar
Robert Schilling committed
650

651 652
        expect(response).to have_gitlab_http_status(200)
        expect(json_response.map { |r| [r['type'], r['name']] }.compact).to eq(refs)
Robert Schilling's avatar
Robert Schilling committed
653 654 655
      end

      it 'returns the branch refs' do
656
        get api(route, current_user), type: 'branch', per_page: 100
Robert Schilling's avatar
Robert Schilling committed
657

658
        refs = project.repository.branch_names_contains(commit_id).map {|name| ['branch', name]}
Robert Schilling's avatar
Robert Schilling committed
659

660 661
        expect(response).to have_gitlab_http_status(200)
        expect(json_response.map { |r| [r['type'], r['name']] }.compact).to eq(refs)
Robert Schilling's avatar
Robert Schilling committed
662 663 664
      end

      it 'returns the tag refs' do
665
        get api(route, current_user), type: 'tag', per_page: 100
Robert Schilling's avatar
Robert Schilling committed
666

667
        refs = project.repository.tag_names_contains(commit_id).map {|name| ['tag', name]}
Robert Schilling's avatar
Robert Schilling committed
668

669 670
        expect(response).to have_gitlab_http_status(200)
        expect(json_response.map { |r| [r['type'], r['name']] }.compact).to eq(refs)
Robert Schilling's avatar
Robert Schilling committed
671 672 673 674
      end
    end
  end

675 676 677 678
  describe 'GET /projects/:id/repository/commits/:sha' do
    let(:commit) { project.repository.commit }
    let(:commit_id) { commit.id }
    let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}" }
679

680 681 682 683 684 685
    shared_examples_for 'ref commit' do
      it 'returns the ref last commit' do
        get api(route, current_user)

        expect(response).to have_gitlab_http_status(200)
        expect(response).to match_response_schema('public_api/v4/commit/detail')
686 687 688 689 690 691 692 693 694 695 696 697 698 699
        expect(json_response['id']).to eq(commit.id)
        expect(json_response['short_id']).to eq(commit.short_id)
        expect(json_response['title']).to eq(commit.title)
        expect(json_response['message']).to eq(commit.safe_message)
        expect(json_response['author_name']).to eq(commit.author_name)
        expect(json_response['author_email']).to eq(commit.author_email)
        expect(json_response['authored_date']).to eq(commit.authored_date.iso8601(3))
        expect(json_response['committer_name']).to eq(commit.committer_name)
        expect(json_response['committer_email']).to eq(commit.committer_email)
        expect(json_response['committed_date']).to eq(commit.committed_date.iso8601(3))
        expect(json_response['parent_ids']).to eq(commit.parent_ids)
        expect(json_response['stats']['additions']).to eq(commit.stats.additions)
        expect(json_response['stats']['deletions']).to eq(commit.stats.deletions)
        expect(json_response['stats']['total']).to eq(commit.stats.total)
700
        expect(json_response['status']).to be_nil
701
        expect(json_response['last_pipeline']).to be_nil
702 703
      end

704 705 706 707 708 709 710
      context 'when ref does not exist' do
        let(:commit_id) { 'unknown' }

        it_behaves_like '404 response' do
          let(:request) { get api(route, current_user) }
          let(:message) { '404 Commit Not Found' }
        end
711
      end
712

713 714
      context 'when repository is disabled' do
        include_context 'disabled repository'
715

716 717 718
        it_behaves_like '403 response' do
          let(:request) { get api(route, current_user) }
        end
719
      end
720
    end
721

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
    context 'when stat param' do
      let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}" }

      it 'is not present return stats by default' do
        get api(route, user)

        expect(response).to have_gitlab_http_status(200)
        expect(json_response).to include 'stats'
      end

      it "is false it does not include stats" do
        get api(route, user), stats: false

        expect(response).to have_gitlab_http_status(200)
        expect(json_response).not_to include 'stats'
      end

      it "is true it includes stats" do
        get api(route, user), stats: true

        expect(response).to have_gitlab_http_status(200)
        expect(json_response).to include 'stats'
      end
    end

747 748
    context 'when unauthenticated', 'and project is public' do
      let(:project) { create(:project, :public, :repository) }
749

750 751
      it_behaves_like 'ref commit'
    end
752

753 754 755 756
    context 'when unauthenticated', 'and project is private' do
      it_behaves_like '404 response' do
        let(:request) { get api(route) }
        let(:message) { '404 Project Not Found' }
757
      end
758
    end
759

760
    context 'when authenticated', 'as a maintainer' do
761
      let(:current_user) { user }
762

763
      it_behaves_like 'ref commit'
764

765 766 767 768 769
      context 'when branch contains a dot' do
        let(:commit) { project.repository.commit(branch_with_dot.name) }
        let(:commit_id) { branch_with_dot.name }

        it_behaves_like 'ref commit'
770
      end
771

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
      context 'when branch contains a slash' do
        let(:commit_id) { branch_with_slash.name }

        it_behaves_like '404 response' do
          let(:request) { get api(route, current_user) }
        end
      end

      context 'when branch contains an escaped slash' do
        let(:commit) { project.repository.commit(branch_with_slash.name) }
        let(:commit_id) { CGI.escape(branch_with_slash.name) }

        it_behaves_like 'ref commit'
      end

      context 'requesting with the escaped project full path' do
        let(:project_id) { CGI.escape(project.full_path) }

        it_behaves_like 'ref commit'

        context 'when branch contains a dot' do
          let(:commit) { project.repository.commit(branch_with_dot.name) }
          let(:commit_id) { branch_with_dot.name }

          it_behaves_like 'ref commit'
        end
      end

      context 'when the ref has a pipeline' do
Shinya Maeda's avatar
Shinya Maeda committed
801
        let!(:pipeline) { project.pipelines.create(source: :push, ref: 'master', sha: commit.sha, protected: false) }
802 803 804 805

        it 'includes a "created" status' do
          get api(route, current_user)

806
          expect(response).to have_gitlab_http_status(200)
807 808
          expect(response).to match_response_schema('public_api/v4/commit/detail')
          expect(json_response['status']).to eq('created')
809 810 811 812
          expect(json_response['last_pipeline']['id']).to eq(pipeline.id)
          expect(json_response['last_pipeline']['ref']).to eq(pipeline.ref)
          expect(json_response['last_pipeline']['sha']).to eq(pipeline.sha)
          expect(json_response['last_pipeline']['status']).to eq(pipeline.status)
813 814 815 816 817 818 819 820 821 822
        end

        context 'when pipeline succeeds' do
          before do
            pipeline.update(status: 'success')
          end

          it 'includes a "success" status' do
            get api(route, current_user)

823
            expect(response).to have_gitlab_http_status(200)
824 825 826 827
            expect(response).to match_response_schema('public_api/v4/commit/detail')
            expect(json_response['status']).to eq('success')
          end
        end
828 829 830 831
      end
    end
  end

832 833 834 835 836 837 838 839 840 841
  describe 'GET /projects/:id/repository/commits/:sha/diff' do
    let(:commit) { project.repository.commit }
    let(:commit_id) { commit.id }
    let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}/diff" }

    shared_examples_for 'ref diff' do
      it 'returns the diff of the selected commit' do
        get api(route, current_user)

        expect(response).to have_gitlab_http_status(200)
842
        expect(response).to include_pagination_headers
843 844
        expect(json_response.size).to be >= 1
        expect(json_response.first.keys).to include 'diff'
845
      end
846

847 848
      context 'when ref does not exist' do
        let(:commit_id) { 'unknown' }
849

850 851 852 853
        it_behaves_like '404 response' do
          let(:request) { get api(route, current_user) }
          let(:message) { '404 Commit Not Found' }
        end
854 855
      end

856 857 858 859 860 861
      context 'when repository is disabled' do
        include_context 'disabled repository'

        it_behaves_like '403 response' do
          let(:request) { get api(route, current_user) }
        end
862 863 864
      end
    end

865 866 867 868 869 870 871 872 873 874 875 876 877
    context 'when unauthenticated', 'and project is public' do
      let(:project) { create(:project, :public, :repository) }

      it_behaves_like 'ref diff'
    end

    context 'when unauthenticated', 'and project is private' do
      it_behaves_like '404 response' do
        let(:request) { get api(route) }
        let(:message) { '404 Project Not Found' }
      end
    end

878
    context 'when authenticated', 'as a maintainer' do
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
      let(:current_user) { user }

      it_behaves_like 'ref diff'

      context 'when branch contains a dot' do
        let(:commit_id) { branch_with_dot.name }

        it_behaves_like 'ref diff'
      end

      context 'when branch contains a slash' do
        let(:commit_id) { branch_with_slash.name }

        it_behaves_like '404 response' do
          let(:request) { get api(route, current_user) }
        end
      end

      context 'when branch contains an escaped slash' do
        let(:commit_id) { CGI.escape(branch_with_slash.name) }

        it_behaves_like 'ref diff'
      end

      context 'requesting with the escaped project full path' do
        let(:project_id) { CGI.escape(project.full_path) }

        it_behaves_like 'ref diff'

        context 'when branch contains a dot' do
          let(:commit_id) { branch_with_dot.name }

          it_behaves_like 'ref diff'
        end
913
      end
914 915 916 917 918 919

      context 'when binary diff are treated as text' do
        let(:commit_id) { TestEnv::BRANCH_SHA['add-pdf-text-binary'] }

        it_behaves_like 'ref diff'
      end
920 921
    end
  end
922

923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
  describe 'GET /projects/:id/repository/commits/:sha/comments' do
    let(:commit) { project.repository.commit }
    let(:commit_id) { commit.id }
    let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}/comments" }

    shared_examples_for 'ref comments' do
      context 'when ref exists' do
        before do
          create(:note_on_commit, author: user, project: project, commit_id: commit.id, note: 'a comment on a commit')
          create(:note_on_commit, author: user, project: project, commit_id: commit.id, note: 'another comment on a commit')
        end

        it 'returns the diff of the selected commit' do
          get api(route, current_user)

          expect(response).to have_gitlab_http_status(200)
          expect(response).to match_response_schema('public_api/v4/commit_notes')
          expect(json_response.size).to eq(2)
          expect(json_response.first['note']).to eq('a comment on a commit')
          expect(json_response.first['author']['id']).to eq(user.id)
        end
944 945
      end

946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
      context 'when ref does not exist' do
        let(:commit_id) { 'unknown' }

        it_behaves_like '404 response' do
          let(:request) { get api(route, current_user) }
          let(:message) { '404 Commit Not Found' }
        end
      end

      context 'when repository is disabled' do
        include_context 'disabled repository'

        it_behaves_like '403 response' do
          let(:request) { get api(route, current_user) }
        end
      end
    end

    context 'when unauthenticated', 'and project is public' do
      let(:project) { create(:project, :public, :repository) }

      it_behaves_like 'ref comments'
    end

    context 'when unauthenticated', 'and project is private' do
      it_behaves_like '404 response' do
        let(:request) { get api(route) }
        let(:message) { '404 Project Not Found' }
974 975 976
      end
    end

977
    context 'when authenticated', 'as a maintainer' do
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
      let(:current_user) { user }

      it_behaves_like 'ref comments'

      context 'when branch contains a dot' do
        let(:commit) { project.repository.commit(branch_with_dot.name) }
        let(:commit_id) { branch_with_dot.name }

        it_behaves_like 'ref comments'
      end

      context 'when branch contains a slash' do
        let(:commit) { project.repository.commit(branch_with_slash.name) }
        let(:commit_id) { branch_with_slash.name }

        it_behaves_like '404 response' do
          let(:request) { get api(route, current_user) }
        end
      end

      context 'when branch contains an escaped slash' do
        let(:commit) { project.repository.commit(branch_with_slash.name) }
        let(:commit_id) { CGI.escape(branch_with_slash.name) }

        it_behaves_like 'ref comments'
      end

      context 'requesting with the escaped project full path' do
        let(:project_id) { CGI.escape(project.full_path) }

        it_behaves_like 'ref comments'

        context 'when branch contains a dot' do
          let(:commit) { project.repository.commit(branch_with_dot.name) }
          let(:commit_id) { branch_with_dot.name }

          it_behaves_like 'ref comments'
        end
1016 1017
      end
    end
1018 1019

    context 'when the commit is present on two projects' do
1020 1021 1022 1023
      let(:forked_project) { create(:project, :repository, creator: guest, namespace: guest.namespace) }
      let!(:forked_project_note) { create(:note_on_commit, author: guest, project: forked_project, commit_id: forked_project.repository.commit.id, note: 'a comment on a commit for fork') }
      let(:project_id) { forked_project.id }
      let(:commit_id) { forked_project.repository.commit.id }
1024 1025

      it 'returns the comments for the target project' do
1026
        get api(route, guest)
1027

1028 1029 1030
        expect(response).to have_gitlab_http_status(200)
        expect(response).to match_response_schema('public_api/v4/commit_notes')
        expect(json_response.size).to eq(1)
1031
        expect(json_response.first['note']).to eq('a comment on a commit for fork')
1032
        expect(json_response.first['author']['id']).to eq(guest.id)
1033 1034
      end
    end
1035 1036
  end

1037
  describe 'POST :id/repository/commits/:sha/cherry_pick' do
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    let(:commit) { project.commit('7d3b0f7cff5f37573aea97cebfd5692ea1689924') }
    let(:commit_id) { commit.id }
    let(:branch) { 'master' }
    let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}/cherry_pick" }

    shared_examples_for 'ref cherry-pick' do
      context 'when ref exists' do
        it 'cherry-picks the ref commit' do
          post api(route, current_user), branch: branch

          expect(response).to have_gitlab_http_status(201)
          expect(response).to match_response_schema('public_api/v4/commit/basic')
          expect(json_response['title']).to eq(commit.title)
1051
          expect(json_response['message']).to eq(commit.cherry_pick_message(user))
1052 1053 1054 1055
          expect(json_response['author_name']).to eq(commit.author_name)
          expect(json_response['committer_name']).to eq(user.name)
        end
      end
1056

1057 1058
      context 'when repository is disabled' do
        include_context 'disabled repository'
1059

1060 1061 1062
        it_behaves_like '403 response' do
          let(:request) { post api(route, current_user), branch: 'master' }
        end
1063
      end
1064
    end
1065

1066 1067
    context 'when unauthenticated', 'and project is public' do
      let(:project) { create(:project, :public, :repository) }
1068

1069 1070 1071 1072 1073 1074 1075 1076 1077
      it_behaves_like '403 response' do
        let(:request) { post api(route), branch: 'master' }
      end
    end

    context 'when unauthenticated', 'and project is private' do
      it_behaves_like '404 response' do
        let(:request) { post api(route), branch: 'master' }
        let(:message) { '404 Project Not Found' }
1078
      end
1079
    end
1080

1081 1082
    context 'when authenticated', 'as an owner' do
      let(:current_user) { user }
1083

1084
      it_behaves_like 'ref cherry-pick'
1085

1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
      context 'when ref does not exist' do
        let(:commit_id) { 'unknown' }

        it_behaves_like '404 response' do
          let(:request) { post api(route, current_user), branch: 'master' }
          let(:message) { '404 Commit Not Found' }
        end
      end

      context 'when branch is missing' do
        it_behaves_like '400 response' do
          let(:request) { post api(route, current_user) }
        end
1099 1100
      end

1101 1102 1103 1104 1105 1106 1107 1108
      context 'when branch is empty' do
        ['', ' '].each do |branch|
          it_behaves_like '400 response' do
            let(:request) { post api(route, current_user), branch: branch }
          end
        end
      end

1109 1110 1111 1112 1113 1114
      context 'when branch does not exist' do
        it_behaves_like '404 response' do
          let(:request) { post api(route, current_user), branch: 'foo' }
          let(:message) { '404 Branch Not Found' }
        end
      end
1115

1116 1117 1118 1119
      context 'when commit is already included in the target branch' do
        it_behaves_like '400 response' do
          let(:request) { post api(route, current_user), branch: 'markdown' }
        end
1120 1121
      end

1122 1123 1124
      context 'when ref contains a dot' do
        let(:commit) { project.repository.commit(branch_with_dot.name) }
        let(:commit_id) { branch_with_dot.name }
1125

1126
        it_behaves_like 'ref cherry-pick'
1127 1128
      end

1129 1130
      context 'when ref contains a slash' do
        let(:commit_id) { branch_with_slash.name }
1131

1132 1133 1134
        it_behaves_like '404 response' do
          let(:request) { post api(route, current_user), branch: 'master' }
        end
1135 1136
      end

1137 1138
      context 'requesting with the escaped project full path' do
        let(:project_id) { CGI.escape(project.full_path) }
1139

1140 1141 1142 1143 1144 1145 1146 1147
        it_behaves_like 'ref cherry-pick'

        context 'when ref contains a dot' do
          let(:commit) { project.repository.commit(branch_with_dot.name) }
          let(:commit_id) { branch_with_dot.name }

          it_behaves_like 'ref cherry-pick'
        end
1148 1149 1150
      end
    end

1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
    context 'when authenticated', 'as a developer' do
      let(:current_user) { guest }

      before do
        project.add_developer(guest)
      end

      context 'when branch is protected' do
        before do
          create(:protected_branch, project: project, name: 'feature')
        end

        it 'returns 400 if you are not allowed to push to the target branch' do
          post api(route, current_user), branch: 'feature'
1165

1166 1167
          expect(response).to have_gitlab_http_status(:forbidden)
          expect(json_response['message']).to match(/You are not allowed to push into this branch/)
1168
        end
1169 1170
      end
    end
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188

    context 'when cherry picking to a fork as a maintainer' do
      include_context 'merge request allowing collaboration'

      let(:project_id) { forked_project.id }

      it 'allows access from a maintainer that to the source branch' do
        post api(route, user), branch: 'feature'

        expect(response).to have_gitlab_http_status(:created)
      end

      it 'denies cherry picking to another branch' do
        post api(route, user), branch: 'master'

        expect(response).to have_gitlab_http_status(:forbidden)
      end
    end
1189 1190
  end

1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
  describe 'POST /projects/:id/repository/commits/:sha/comments' do
    let(:commit) { project.repository.commit }
    let(:commit_id) { commit.id }
    let(:note) { 'My comment' }
    let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}/comments" }

    shared_examples_for 'ref new comment' do
      context 'when ref exists' do
        it 'creates the comment' do
          post api(route, current_user), note: note

          expect(response).to have_gitlab_http_status(201)
          expect(response).to match_response_schema('public_api/v4/commit_note')
          expect(json_response['note']).to eq('My comment')
          expect(json_response['path']).to be_nil
          expect(json_response['line']).to be_nil
          expect(json_response['line_type']).to be_nil
        end
1209 1210
      end

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
      context 'when repository is disabled' do
        include_context 'disabled repository'

        it_behaves_like '403 response' do
          let(:request) { post api(route, current_user), note: 'My comment' }
        end
      end
    end

    context 'when unauthenticated', 'and project is public' do
      let(:project) { create(:project, :public, :repository) }

      it_behaves_like '400 response' do
        let(:request) { post api(route), note: 'My comment' }
      end
    end

    context 'when unauthenticated', 'and project is private' do
      it_behaves_like '404 response' do
        let(:request) { post api(route), note: 'My comment' }
        let(:message) { '404 Project Not Found' }
      end
    end

    context 'when authenticated', 'as an owner' do
      let(:current_user) { user }

      it_behaves_like 'ref new comment'

1240
      it 'returns the inline comment' do
1241
        post api(route, current_user), note: 'My comment', path: project.repository.commit.raw_diffs.first.new_path, line: 1, line_type: 'new'
1242

1243 1244
        expect(response).to have_gitlab_http_status(201)
        expect(response).to match_response_schema('public_api/v4/commit_note')
1245
        expect(json_response['note']).to eq('My comment')
1246
        expect(json_response['path']).to eq(project.repository.commit.raw_diffs.first.new_path)
1247
        expect(json_response['line']).to eq(1)
1248
        expect(json_response['line_type']).to eq('new')
1249 1250
      end

1251 1252 1253 1254 1255 1256 1257 1258 1259
      context 'when ref does not exist' do
        let(:commit_id) { 'unknown' }

        it_behaves_like '404 response' do
          let(:request) { post api(route, current_user), note: 'My comment' }
          let(:message) { '404 Commit Not Found' }
        end
      end

1260
      it 'returns 400 if note is missing' do
1261 1262 1263
        post api(route, current_user)

        expect(response).to have_gitlab_http_status(400)
1264 1265
      end

1266 1267 1268 1269
      context 'when ref contains a dot' do
        let(:commit_id) { branch_with_dot.name }

        it_behaves_like 'ref new comment'
1270 1271
      end

1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
      context 'when ref contains a slash' do
        let(:commit_id) { branch_with_slash.name }

        it_behaves_like '404 response' do
          let(:request) { post api(route, current_user), note: 'My comment' }
        end
      end

      context 'when ref contains an escaped slash' do
        let(:commit_id) { CGI.escape(branch_with_slash.name) }

        it_behaves_like 'ref new comment'
      end

      context 'requesting with the escaped project full path' do
        let(:project_id) { CGI.escape(project.full_path) }

        it_behaves_like 'ref new comment'

        context 'when ref contains a dot' do
          let(:commit_id) { branch_with_dot.name }

          it_behaves_like 'ref new comment'
        end
1296 1297 1298
      end
    end
  end
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327

  describe 'GET /projects/:id/repository/commits/:sha/merge_requests' do
    let!(:project) { create(:project, :repository, :private) }
    let!(:merged_mr) { create(:merge_request, source_project: project, source_branch: 'master', target_branch: 'feature') }
    let(:commit) { merged_mr.merge_request_diff.commits.last }

    it 'returns the correct merge request' do
      get api("/projects/#{project.id}/repository/commits/#{commit.id}/merge_requests", user)

      expect(response).to have_gitlab_http_status(200)
      expect(response).to include_pagination_headers
      expect(json_response.length).to eq(1)
      expect(json_response[0]['id']).to eq(merged_mr.id)
    end

    it 'returns 403 for an unauthorized user' do
      project.add_guest(user)

      get api("/projects/#{project.id}/repository/commits/#{commit.id}/merge_requests", user)

      expect(response).to have_gitlab_http_status(403)
    end

    it 'responds 404 when the commit does not exist' do
      get api("/projects/#{project.id}/repository/commits/a7d26f00c35b/merge_requests", user)

      expect(response).to have_gitlab_http_status(404)
    end
  end
1328
end