summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/changelog/committer_spec.rb
blob: 1e04fe346cb9adbbe64ca86481a22e052d84758e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Changelog::Committer do
  let(:project) { create(:project, :repository) }
  let(:user) { project.creator }
  let(:committer) { described_class.new(project, user) }
  let(:config) { Gitlab::Changelog::Config.new(project) }

  describe '#commit' do
    context "when the release isn't in the changelog" do
      it 'commits the changes' do
        release = Gitlab::Changelog::Release
          .new(version: '1.0.0', date: Time.utc(2020, 1, 1), config: config)

        committer.commit(
          release: release,
          file: 'CHANGELOG.md',
          branch: 'master',
          message: 'Test commit'
        )

        content = project.repository.blob_at('master', 'CHANGELOG.md').data

        expect(content).to eq(<<~MARKDOWN)
          ## 1.0.0 (2020-01-01)

          No changes.
        MARKDOWN
      end
    end

    context 'when the release is already in the changelog' do
      it "doesn't commit the changes" do
        release = Gitlab::Changelog::Release
          .new(version: '1.0.0', date: Time.utc(2020, 1, 1), config: config)

        2.times do
          committer.commit(
            release: release,
            file: 'CHANGELOG.md',
            branch: 'master',
            message: 'Test commit'
          )
        end

        content = project.repository.blob_at('master', 'CHANGELOG.md').data

        expect(content).to eq(<<~MARKDOWN)
          ## 1.0.0 (2020-01-01)

          No changes.
        MARKDOWN
      end
    end

    context 'when committing the changes fails' do
      it 'retries the operation' do
        release = Gitlab::Changelog::Release
          .new(version: '1.0.0', date: Time.utc(2020, 1, 1), config: config)

        service = instance_spy(Files::MultiService)
        errored = false

        allow(Files::MultiService)
          .to receive(:new)
          .and_return(service)

        allow(service).to receive(:execute) do
          if errored
            { status: :success }
          else
            errored = true
            { status: :error }
          end
        end

        expect do
          committer.commit(
            release: release,
            file: 'CHANGELOG.md',
            branch: 'master',
            message: 'Test commit'
          )
        end.not_to raise_error
      end
    end

    context "when the changelog changes before saving the changes" do
      it 'raises a Error' do
        release1 = Gitlab::Changelog::Release
          .new(version: '1.0.0', date: Time.utc(2020, 1, 1), config: config)

        release2 = Gitlab::Changelog::Release
          .new(version: '2.0.0', date: Time.utc(2020, 1, 1), config: config)

        # This creates the initial commit we'll later use to see if the
        # changelog changed before saving our changes.
        committer.commit(
          release: release1,
          file: 'CHANGELOG.md',
          branch: 'master',
          message: 'Initial commit'
        )

        allow(Gitlab::Git::Commit)
          .to receive(:last_for_path)
          .with(
            project.repository,
            'master',
            'CHANGELOG.md',
            literal_pathspec: true
          )
          .and_return(double(:commit, sha: 'foo'))

        expect do
          committer.commit(
            release: release2,
            file: 'CHANGELOG.md',
            branch: 'master',
            message: 'Test commit'
          )
        end.to raise_error(Gitlab::Changelog::Error)
      end
    end
  end
end