commits_spec.rb 42.4 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
L
Luis HGO 已提交
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
L
Luis HGO 已提交
131

132
          get api("/projects/#{project_id}/repository/commits?path=#{path}", user)
L
Luis HGO 已提交
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

T
Tiago Botelho 已提交
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" }
M
Marc Siegfriedt 已提交
226 227

    it 'returns a 403 unauthorized for user without permissions' do
228
      post api(url, guest)
M
Marc Siegfriedt 已提交
229

230
      expect(response).to have_gitlab_http_status(403)
M
Marc Siegfriedt 已提交
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)
M
Marc Siegfriedt 已提交
237 238
    end

239
    describe 'create' do
M
Marc Siegfriedt 已提交
240
      let(:message) { 'Created file' }
241
      let(:invalid_c_params) do
M
Marc Siegfriedt 已提交
242
        {
243
          branch: 'master',
M
Marc Siegfriedt 已提交
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
M
Marc Siegfriedt 已提交
255
        {
256
          branch: 'master',
M
Marc Siegfriedt 已提交
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
M
Marc Siegfriedt 已提交
280

T
Tiago Botelho 已提交
281
      it 'does not increment the usage counters using access token authentication' do
282
        expect(::Gitlab::WebIdeCommitsCounter).not_to receive(:increment)
T
Tiago Botelho 已提交
283

284
        post api(url, user), valid_c_params
T
Tiago Botelho 已提交
285 286
      end

M
Marc Siegfriedt 已提交
287 288 289
      it 'a new file in project repo' do
        post api(url, user), valid_c_params

290
        expect(response).to have_gitlab_http_status(201)
M
Marc Siegfriedt 已提交
291
        expect(json_response['title']).to eq(message)
292 293
        expect(json_response['committer_name']).to eq(user.name)
        expect(json_response['committer_email']).to eq(user.email)
M
Marc Siegfriedt 已提交
294 295
      end

296 297 298 299 300 301 302 303 304
      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

M
Marc Siegfriedt 已提交
305 306 307
      it 'returns a 400 bad request if file exists' do
        post api(url, user), invalid_c_params

308
        expect(response).to have_gitlab_http_status(400)
M
Marc Siegfriedt 已提交
309
      end
310

311 312
      context 'with project path containing a dot in URL' do
        let(:url) { "/projects/#{CGI.escape(project.full_path)}/repository/commits" }
313 314 315 316

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

317
          expect(response).to have_gitlab_http_status(201)
318 319
        end
      end
M
Marc Siegfriedt 已提交
320 321
    end

322
    describe 'delete' do
M
Marc Siegfriedt 已提交
323
      let(:message) { 'Deleted file' }
324
      let(:invalid_d_params) do
M
Marc Siegfriedt 已提交
325
        {
326
          branch: 'markdown',
M
Marc Siegfriedt 已提交
327 328 329 330 331 332 333 334 335
          commit_message: message,
          actions: [
            {
              action: 'delete',
              file_path: 'doc/api/projects.md'
            }
          ]
        }
      end
336
      let(:valid_d_params) do
M
Marc Siegfriedt 已提交
337
        {
338
          branch: 'markdown',
M
Marc Siegfriedt 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351
          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

352
        expect(response).to have_gitlab_http_status(201)
M
Marc Siegfriedt 已提交
353 354 355 356 357 358
        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

359
        expect(response).to have_gitlab_http_status(400)
M
Marc Siegfriedt 已提交
360 361 362
      end
    end

363
    describe 'move' do
M
Marc Siegfriedt 已提交
364
      let(:message) { 'Moved file' }
365
      let(:invalid_m_params) do
M
Marc Siegfriedt 已提交
366
        {
367
          branch: 'feature',
M
Marc Siegfriedt 已提交
368 369 370 371 372 373 374 375 376 377 378
          commit_message: message,
          actions: [
            {
              action: 'move',
              file_path: 'CHANGELOG',
              previous_path: 'VERSION',
              content: '6.7.0.pre'
            }
          ]
        }
      end
379
      let(:valid_m_params) do
M
Marc Siegfriedt 已提交
380
        {
381
          branch: 'feature',
M
Marc Siegfriedt 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
          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

397
        expect(response).to have_gitlab_http_status(201)
M
Marc Siegfriedt 已提交
398 399 400 401 402 403
        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

404
        expect(response).to have_gitlab_http_status(400)
M
Marc Siegfriedt 已提交
405 406 407
      end
    end

408
    describe 'update' do
M
Marc Siegfriedt 已提交
409
      let(:message) { 'Updated file' }
410
      let(:invalid_u_params) do
M
Marc Siegfriedt 已提交
411
        {
412
          branch: 'master',
M
Marc Siegfriedt 已提交
413 414 415 416 417 418 419 420 421 422
          commit_message: message,
          actions: [
            {
              action: 'update',
              file_path: 'foo/bar.baz',
              content: 'puts 8'
            }
          ]
        }
      end
423
      let(:valid_u_params) do
M
Marc Siegfriedt 已提交
424
        {
425
          branch: 'master',
M
Marc Siegfriedt 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439
          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

440
        expect(response).to have_gitlab_http_status(201)
M
Marc Siegfriedt 已提交
441 442 443 444 445 446
        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

447
        expect(response).to have_gitlab_http_status(400)
M
Marc Siegfriedt 已提交
448 449 450
      end
    end

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 493 494 495 496 497 498
    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

499
    describe 'multiple operations' do
M
Marc Siegfriedt 已提交
500
      let(:message) { 'Multiple actions' }
501
      let(:invalid_mo_params) do
M
Marc Siegfriedt 已提交
502
        {
503
          branch: 'master',
M
Marc Siegfriedt 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
          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'
525 526 527 528 529
            },
            {
              action: 'chmod',
              file_path: 'files/ruby/popen.rb',
              execute_filemode: true
M
Marc Siegfriedt 已提交
530 531 532 533
            }
          ]
        }
      end
534
      let(:valid_mo_params) do
M
Marc Siegfriedt 已提交
535
        {
536
          branch: 'master',
M
Marc Siegfriedt 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
          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'
558 559 560 561 562
            },
            {
              action: 'chmod',
              file_path: 'files/ruby/popen.rb',
              execute_filemode: true
M
Marc Siegfriedt 已提交
563 564 565 566 567 568 569 570
            }
          ]
        }
      end

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

571
        expect(response).to have_gitlab_http_status(201)
M
Marc Siegfriedt 已提交
572 573 574 575 576 577
        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

578
        expect(response).to have_gitlab_http_status(400)
M
Marc Siegfriedt 已提交
579 580
      end
    end
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 607 608 609 610 611 612

    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
M
Marc Siegfriedt 已提交
613 614
  end

R
Robert Schilling 已提交
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
  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
640
        get api(route, current_user), per_page: 100
R
Robert Schilling 已提交
641

642 643
        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]})
R
Robert Schilling 已提交
644

645 646 647 648
        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)
R
Robert Schilling 已提交
649 650 651
      end

      it 'returns all refs' do
652
        get api(route, current_user), type: 'all', per_page: 100
R
Robert Schilling 已提交
653

654 655
        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]})
R
Robert Schilling 已提交
656

657 658
        expect(response).to have_gitlab_http_status(200)
        expect(json_response.map { |r| [r['type'], r['name']] }.compact).to eq(refs)
R
Robert Schilling 已提交
659 660 661
      end

      it 'returns the branch refs' do
662
        get api(route, current_user), type: 'branch', per_page: 100
R
Robert Schilling 已提交
663

664
        refs = project.repository.branch_names_contains(commit_id).map {|name| ['branch', name]}
R
Robert Schilling 已提交
665

666 667
        expect(response).to have_gitlab_http_status(200)
        expect(json_response.map { |r| [r['type'], r['name']] }.compact).to eq(refs)
R
Robert Schilling 已提交
668 669 670
      end

      it 'returns the tag refs' do
671
        get api(route, current_user), type: 'tag', per_page: 100
R
Robert Schilling 已提交
672

673
        refs = project.repository.tag_names_contains(commit_id).map {|name| ['tag', name]}
R
Robert Schilling 已提交
674

675 676
        expect(response).to have_gitlab_http_status(200)
        expect(json_response.map { |r| [r['type'], r['name']] }.compact).to eq(refs)
R
Robert Schilling 已提交
677 678 679 680
      end
    end
  end

681 682 683 684
  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}" }
D
dixpac 已提交
685

686 687 688 689 690 691
    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')
692 693 694 695 696 697 698 699 700 701 702 703 704 705
        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)
706
        expect(json_response['status']).to be_nil
707
        expect(json_response['last_pipeline']).to be_nil
708 709
      end

710 711 712 713 714 715 716
      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
717
      end
K
Kamil Trzcinski 已提交
718

719 720
      context 'when repository is disabled' do
        include_context 'disabled repository'
721

722 723 724
        it_behaves_like '403 response' do
          let(:request) { get api(route, current_user) }
        end
K
Kamil Trzcinski 已提交
725
      end
726
    end
K
Kamil Trzcinski 已提交
727

728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
    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

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

756 757
      it_behaves_like 'ref commit'
    end
758

759 760 761 762
    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' }
K
Kamil Trzcinski 已提交
763
      end
764
    end
765

766
    context 'when authenticated', 'as a maintainer' do
767
      let(:current_user) { user }
768

769
      it_behaves_like 'ref commit'
770

771 772 773 774 775
      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'
776
      end
777

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
      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
S
Shinya Maeda 已提交
807
        let!(:pipeline) { project.pipelines.create(source: :push, ref: 'master', sha: commit.sha, protected: false) }
808 809 810 811

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

812
          expect(response).to have_gitlab_http_status(200)
813 814
          expect(response).to match_response_schema('public_api/v4/commit/detail')
          expect(json_response['status']).to eq('created')
815 816 817 818
          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)
819 820 821 822 823 824 825 826 827 828
        end

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

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

829
            expect(response).to have_gitlab_http_status(200)
830 831 832 833
            expect(response).to match_response_schema('public_api/v4/commit/detail')
            expect(json_response['status']).to eq('success')
          end
        end
834 835 836 837
      end
    end
  end

838 839 840 841 842 843 844 845 846 847
  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)
848
        expect(response).to include_pagination_headers
849 850
        expect(json_response.size).to be >= 1
        expect(json_response.first.keys).to include 'diff'
851
      end
852

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

856 857 858 859
        it_behaves_like '404 response' do
          let(:request) { get api(route, current_user) }
          let(:message) { '404 Commit Not Found' }
        end
860 861
      end

862 863 864 865 866 867
      context 'when repository is disabled' do
        include_context 'disabled repository'

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

871 872 873 874 875 876 877 878 879 880 881 882 883
    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

884
    context 'when authenticated', 'as a maintainer' do
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 913 914 915 916 917 918
      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
919
      end
920 921 922 923 924 925

      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
926 927
    end
  end
928

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
  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
950 951
      end

952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
      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' }
980 981 982
      end
    end

983
    context 'when authenticated', 'as a maintainer' do
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 1016 1017 1018 1019 1020 1021
      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
1022 1023
      end
    end
1024 1025

    context 'when the commit is present on two projects' do
1026 1027 1028 1029
      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 }
1030 1031

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

1034 1035 1036
        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)
1037
        expect(json_response.first['note']).to eq('a comment on a commit for fork')
1038
        expect(json_response.first['author']['id']).to eq(guest.id)
1039 1040
      end
    end
1041 1042
  end

1043
  describe 'POST :id/repository/commits/:sha/cherry_pick' do
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
    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)
1057
          expect(json_response['message']).to eq(commit.cherry_pick_message(user))
1058 1059 1060 1061
          expect(json_response['author_name']).to eq(commit.author_name)
          expect(json_response['committer_name']).to eq(user.name)
        end
      end
1062

1063 1064
      context 'when repository is disabled' do
        include_context 'disabled repository'
1065

1066 1067 1068
        it_behaves_like '403 response' do
          let(:request) { post api(route, current_user), branch: 'master' }
        end
1069
      end
1070
    end
1071

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

1075 1076 1077 1078 1079 1080 1081 1082 1083
      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' }
1084
      end
1085
    end
1086

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

1090
      it_behaves_like 'ref cherry-pick'
1091

1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
      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
1105 1106
      end

1107 1108 1109 1110 1111 1112 1113 1114
      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

1115 1116 1117 1118 1119 1120
      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
1121

1122 1123 1124 1125
      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
1126 1127
      end

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

1132
        it_behaves_like 'ref cherry-pick'
1133 1134
      end

1135 1136
      context 'when ref contains a slash' do
        let(:commit_id) { branch_with_slash.name }
1137

1138 1139 1140
        it_behaves_like '404 response' do
          let(:request) { post api(route, current_user), branch: 'master' }
        end
1141 1142
      end

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

1146 1147 1148 1149 1150 1151 1152 1153
        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
1154 1155 1156
      end
    end

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    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'
1171

1172 1173
          expect(response).to have_gitlab_http_status(:forbidden)
          expect(json_response['message']).to match(/You are not allowed to push into this branch/)
1174
        end
1175 1176
      end
    end
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194

    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
1195 1196
  end

1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
  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
1215 1216
      end

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
      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'

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

1249 1250
        expect(response).to have_gitlab_http_status(201)
        expect(response).to match_response_schema('public_api/v4/commit_note')
1251
        expect(json_response['note']).to eq('My comment')
1252
        expect(json_response['path']).to eq(project.repository.commit.raw_diffs.first.new_path)
1253
        expect(json_response['line']).to eq(1)
1254
        expect(json_response['line_type']).to eq('new')
1255 1256
      end

1257 1258 1259 1260 1261 1262 1263 1264 1265
      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

1266
      it 'returns 400 if note is missing' do
1267 1268 1269
        post api(route, current_user)

        expect(response).to have_gitlab_http_status(400)
1270 1271
      end

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

        it_behaves_like 'ref new comment'
1276 1277
      end

1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
      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
1302 1303 1304
      end
    end
  end
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333

  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
1334
end