summaryrefslogtreecommitdiff
path: root/spec/services/create_commit_builds_service_spec.rb
blob: ea5dcfa068a0ffe114ebc1af7d3c5251b53da1ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require 'spec_helper'

describe CreateCommitBuildsService, services: true do
  let(:service) { CreateCommitBuildsService.new }
  let(:project) { FactoryGirl.create(:empty_project) }
  let(:user) { nil }

  before do
    stub_ci_commit_to_return_yaml_file
  end

  describe :execute do
    context 'valid params' do
      let(:commit) do
        service.execute(project, user,
                        ref: 'refs/heads/master',
                        before: '00000000',
                        after: '31das312',
                        commits: [{ message: "Message" }]
                       )
      end

      it { expect(commit).to be_kind_of(Ci::Commit) }
      it { expect(commit).to be_valid }
      it { expect(commit).to be_persisted }
      it { expect(commit).to eq(project.ci_commits.last) }
      it { expect(commit.builds.first).to be_kind_of(Ci::Build) }
    end

    context "skip tag if there is no build for it" do
      it "creates commit if there is appropriate job" do
        result = service.execute(project, user,
                                 ref: 'refs/tags/0_1',
                                 before: '00000000',
                                 after: '31das312',
                                 commits: [{ message: "Message" }]
                                )
        expect(result).to be_persisted
      end

      it "creates commit if there is no appropriate job but deploy job has right ref setting" do
        config = YAML.dump({ deploy: { deploy: "ls", only: ["0_1"] } })
        stub_ci_commit_yaml_file(config)

        result = service.execute(project, user,
                                 ref: 'refs/heads/0_1',
                                 before: '00000000',
                                 after: '31das312',
                                 commits: [{ message: "Message" }]
                                )
        expect(result).to be_persisted
      end
    end

    it 'skips creating ci_commit for refs without .gitlab-ci.yml' do
      stub_ci_commit_yaml_file(nil)
      result = service.execute(project, user,
                               ref: 'refs/heads/0_1',
                               before: '00000000',
                               after: '31das312',
                               commits: [{ message: 'Message' }]
                              )
      expect(result).to be_falsey
      expect(Ci::Commit.count).to eq(0)
    end

    it 'fails commits if yaml is invalid' do
      message = 'message'
      allow_any_instance_of(Ci::Commit).to receive(:git_commit_message) { message }
      stub_ci_commit_yaml_file('invalid: file: file')
      commits = [{ message: message }]
      commit = service.execute(project, user,
                               ref: 'refs/tags/0_1',
                               before: '00000000',
                               after: '31das312',
                               commits: commits
                              )
      expect(commit).to be_persisted
      expect(commit.builds.any?).to be false
      expect(commit.status).to eq('failed')
      expect(commit.yaml_errors).to_not be_nil
    end

    describe :ci_skip? do
      let(:message) { "some message[ci skip]" }

      before do
        allow_any_instance_of(Ci::Commit).to receive(:git_commit_message) { message }
      end

      it "skips builds creation if there is [ci skip] tag in commit message" do
        commits = [{ message: message }]
        commit = service.execute(project, user,
                                 ref: 'refs/tags/0_1',
                                 before: '00000000',
                                 after: '31das312',
                                 commits: commits
                                )
        expect(commit).to be_persisted
        expect(commit.builds.any?).to be false
        expect(commit.status).to eq("skipped")
      end

      it "does not skips builds creation if there is no [ci skip] tag in commit message" do
        allow_any_instance_of(Ci::Commit).to receive(:git_commit_message) { "some message" }

        commits = [{ message: "some message" }]
        commit = service.execute(project, user,
                                 ref: 'refs/tags/0_1',
                                 before: '00000000',
                                 after: '31das312',
                                 commits: commits
                                )

        expect(commit).to be_persisted
        expect(commit.builds.first.name).to eq("staging")
      end

      it "skips builds creation if there is [ci skip] tag in commit message and yaml is invalid" do
        stub_ci_commit_yaml_file('invalid: file: fiile')
        commits = [{ message: message }]
        commit = service.execute(project, user,
                                 ref: 'refs/tags/0_1',
                                 before: '00000000',
                                 after: '31das312',
                                 commits: commits
                                )
        expect(commit).to be_persisted
        expect(commit.builds.any?).to be false
        expect(commit.status).to eq("skipped")
        expect(commit.yaml_errors).to be_nil
      end
    end

    it "skips build creation if there are already builds" do
      allow_any_instance_of(Ci::Commit).to receive(:ci_yaml_file) { gitlab_ci_yaml }

      commits = [{ message: "message" }]
      commit = service.execute(project, user,
                               ref: 'refs/heads/master',
                               before: '00000000',
                               after: '31das312',
                               commits: commits
                              )
      expect(commit).to be_persisted
      expect(commit.builds.count(:all)).to eq(2)

      commit = service.execute(project, user,
                               ref: 'refs/heads/master',
                               before: '00000000',
                               after: '31das312',
                               commits: commits
                              )
      expect(commit).to be_persisted
      expect(commit.builds.count(:all)).to eq(2)
    end

    it "creates commit with failed status if yaml is invalid" do
      stub_ci_commit_yaml_file('invalid: file')

      commits = [{ message: "some message" }]

      commit = service.execute(project, user,
                               ref: 'refs/tags/0_1',
                               before: '00000000',
                               after: '31das312',
                               commits: commits
                              )

      expect(commit).to be_persisted
      expect(commit.status).to eq("failed")
      expect(commit.builds.any?).to be false
    end
  end
end