summaryrefslogtreecommitdiff
path: root/spec/services/create_commit_service_spec.rb
blob: c04aaa6f3ea20415dcc1260ef97d4b5cbbf84d9f (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
require 'spec_helper'

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

      it { commit.should be_kind_of(Commit) }
      it { commit.should be_valid }
      it { commit.should be_persisted }
      it { commit.should == project.commits.last }
      it { commit.builds.first.should be_kind_of(Build) }
    end

    context "deploy builds" do
      it "calls create_deploy_builds if there are no builds" do
        config = YAML.dump({production: {deploy: "ls"}})
        Commit.any_instance.should_receive(:create_deploy_builds)
        service.execute(project,
          ref: 'refs/heads/master',
          before: '00000000',
          after: '31das312',
          ci_yaml_file: config,
          commits: [ { message: "Message" } ]
        )
      end

      it "does not call create_deploy_builds if there is build" do
        config = YAML.dump({rspec: {test: "ls"},production: {deploy: "ls"}})
        Commit.any_instance.should_not_receive(:create_deploy_builds)
        service.execute(project,
          ref: 'refs/heads/master',
          before: '00000000',
          after: '31das312',
          ci_yaml_file: config,
          commits: [ { message: "Message" } ]
        )
      end
    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,
          ref: 'refs/tags/0_1',
          before: '00000000',
          after: '31das312',
          ci_yaml_file: gitlab_ci_yaml,
          commits: [ { message: "Message" } ]
        )
        result.should be_persisted
      end

      it "does not create commit if there is no appropriate job nor deploy job" do
        result = service.execute(project,
          ref: 'refs/tags/0_1',
          before: '00000000',
          after: '31das312',
          ci_yaml_file: YAML.dump({}),
          commits: [ { message: "Message" } ]
        )
        result.should be_false
      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"]}})

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

    describe :ci_skip? do
      it "skips builds creation if there is [ci skip] tag in commit message" do
        commits = [{message: "some message[ci skip]"}]
        commit = service.execute(project,
          ref: 'refs/tags/0_1',
          before: '00000000',
          after: '31das312',
          commits: commits,
          ci_yaml_file: gitlab_ci_yaml
        )
        commit.builds.any?.should be_false
      end

      it "does not skips builds creation if there is no [ci skip] tag in commit message" do
        commits = [{message: "some message"}]

        commit = service.execute(project,
          ref: 'refs/tags/0_1',
          before: '00000000',
          after: '31das312',
          commits: commits,
          ci_yaml_file: gitlab_ci_yaml
        )
        
        commit.builds.first.name.should == "staging"
      end
    end

    it "skips build creation if there are already builds" do
      commits = [{message: "message"}]
      commit = service.execute(project,
        ref: 'refs/heads/master',
        before: '00000000',
        after: '31das312',
        commits: commits,
        ci_yaml_file: gitlab_ci_yaml
      )
      commit.builds.count(:all).should == 2

      commit = service.execute(project,
        ref: 'refs/heads/master',
        before: '00000000',
        after: '31das312',
        commits: commits,
        ci_yaml_file: gitlab_ci_yaml
      )
      commit.builds.count(:all).should == 2
    end
  end
end