2015-12-23 02:04:40 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe CreateCommitBuildsService, services: true do
|
|
|
|
let(:service) { CreateCommitBuildsService.new }
|
|
|
|
let(:project) { FactoryGirl.create(:empty_project) }
|
|
|
|
let(:user) { nil }
|
|
|
|
|
|
|
|
before do
|
2016-06-16 23:09:34 +05:30
|
|
|
stub_ci_pipeline_to_return_yaml_file
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe :execute do
|
|
|
|
context 'valid params' do
|
2016-06-16 23:09:34 +05:30
|
|
|
let(:pipeline) do
|
2015-12-23 02:04:40 +05:30
|
|
|
service.execute(project, user,
|
|
|
|
ref: 'refs/heads/master',
|
|
|
|
before: '00000000',
|
|
|
|
after: '31das312',
|
|
|
|
commits: [{ message: "Message" }]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
it { expect(pipeline).to be_kind_of(Ci::Pipeline) }
|
|
|
|
it { expect(pipeline).to be_valid }
|
|
|
|
it { expect(pipeline).to be_persisted }
|
|
|
|
it { expect(pipeline).to eq(project.pipelines.last) }
|
|
|
|
it { expect(pipeline.builds.first).to be_kind_of(Ci::Build) }
|
2015-12-23 02:04:40 +05:30
|
|
|
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
|
2016-06-22 15:30:34 +05:30
|
|
|
config = YAML.dump({ deploy: { script: "ls", only: ["0_1"] } })
|
2016-06-16 23:09:34 +05:30
|
|
|
stub_ci_pipeline_yaml_file(config)
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
result = service.execute(project, user,
|
|
|
|
ref: 'refs/heads/0_1',
|
|
|
|
before: '00000000',
|
|
|
|
after: '31das312',
|
|
|
|
commits: [{ message: "Message" }]
|
|
|
|
)
|
|
|
|
expect(result).to be_persisted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
it 'skips creating pipeline for refs without .gitlab-ci.yml' do
|
|
|
|
stub_ci_pipeline_yaml_file(nil)
|
2015-12-23 02:04:40 +05:30
|
|
|
result = service.execute(project, user,
|
|
|
|
ref: 'refs/heads/0_1',
|
|
|
|
before: '00000000',
|
|
|
|
after: '31das312',
|
|
|
|
commits: [{ message: 'Message' }]
|
|
|
|
)
|
|
|
|
expect(result).to be_falsey
|
2016-06-16 23:09:34 +05:30
|
|
|
expect(Ci::Pipeline.count).to eq(0)
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails commits if yaml is invalid' do
|
|
|
|
message = 'message'
|
2016-06-16 23:09:34 +05:30
|
|
|
allow_any_instance_of(Ci::Pipeline).to receive(:git_commit_message) { message }
|
|
|
|
stub_ci_pipeline_yaml_file('invalid: file: file')
|
2015-12-23 02:04:40 +05:30
|
|
|
commits = [{ message: message }]
|
2016-06-16 23:09:34 +05:30
|
|
|
pipeline = service.execute(project, user,
|
|
|
|
ref: 'refs/tags/0_1',
|
|
|
|
before: '00000000',
|
|
|
|
after: '31das312',
|
|
|
|
commits: commits
|
|
|
|
)
|
|
|
|
expect(pipeline).to be_persisted
|
|
|
|
expect(pipeline.builds.any?).to be false
|
|
|
|
expect(pipeline.status).to eq('failed')
|
|
|
|
expect(pipeline.yaml_errors).not_to be_nil
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
context 'when commit contains a [ci skip] directive' do
|
2015-12-23 02:04:40 +05:30
|
|
|
let(:message) { "some message[ci skip]" }
|
|
|
|
|
|
|
|
before do
|
2016-06-16 23:09:34 +05:30
|
|
|
allow_any_instance_of(Ci::Pipeline).to receive(:git_commit_message) { message }
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "skips builds creation if there is [ci skip] tag in commit message" do
|
|
|
|
commits = [{ message: message }]
|
2016-06-16 23:09:34 +05:30
|
|
|
pipeline = service.execute(project, user,
|
|
|
|
ref: 'refs/tags/0_1',
|
|
|
|
before: '00000000',
|
|
|
|
after: '31das312',
|
|
|
|
commits: commits
|
|
|
|
)
|
|
|
|
expect(pipeline).to be_persisted
|
|
|
|
expect(pipeline.builds.any?).to be false
|
|
|
|
expect(pipeline.status).to eq("skipped")
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "does not skips builds creation if there is no [ci skip] tag in commit message" do
|
2016-06-16 23:09:34 +05:30
|
|
|
allow_any_instance_of(Ci::Pipeline).to receive(:git_commit_message) { "some message" }
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
commits = [{ message: "some message" }]
|
2016-06-16 23:09:34 +05:30
|
|
|
pipeline = service.execute(project, user,
|
|
|
|
ref: 'refs/tags/0_1',
|
|
|
|
before: '00000000',
|
|
|
|
after: '31das312',
|
|
|
|
commits: commits
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(pipeline).to be_persisted
|
|
|
|
expect(pipeline.builds.first.name).to eq("staging")
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "skips builds creation if there is [ci skip] tag in commit message and yaml is invalid" do
|
2016-06-16 23:09:34 +05:30
|
|
|
stub_ci_pipeline_yaml_file('invalid: file: fiile')
|
2015-12-23 02:04:40 +05:30
|
|
|
commits = [{ message: message }]
|
2016-06-16 23:09:34 +05:30
|
|
|
pipeline = service.execute(project, user,
|
|
|
|
ref: 'refs/tags/0_1',
|
|
|
|
before: '00000000',
|
|
|
|
after: '31das312',
|
|
|
|
commits: commits
|
|
|
|
)
|
|
|
|
expect(pipeline).to be_persisted
|
|
|
|
expect(pipeline.builds.any?).to be false
|
|
|
|
expect(pipeline.status).to eq("skipped")
|
|
|
|
expect(pipeline.yaml_errors).to be_nil
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "skips build creation if there are already builds" do
|
2016-06-16 23:09:34 +05:30
|
|
|
allow_any_instance_of(Ci::Pipeline).to receive(:ci_yaml_file) { gitlab_ci_yaml }
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
commits = [{ message: "message" }]
|
2016-06-16 23:09:34 +05:30
|
|
|
pipeline = service.execute(project, user,
|
|
|
|
ref: 'refs/heads/master',
|
|
|
|
before: '00000000',
|
|
|
|
after: '31das312',
|
|
|
|
commits: commits
|
|
|
|
)
|
|
|
|
expect(pipeline).to be_persisted
|
|
|
|
expect(pipeline.builds.count(:all)).to eq(2)
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
pipeline = service.execute(project, user,
|
|
|
|
ref: 'refs/heads/master',
|
|
|
|
before: '00000000',
|
|
|
|
after: '31das312',
|
|
|
|
commits: commits
|
|
|
|
)
|
|
|
|
expect(pipeline).to be_persisted
|
|
|
|
expect(pipeline.builds.count(:all)).to eq(2)
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "creates commit with failed status if yaml is invalid" do
|
2016-06-16 23:09:34 +05:30
|
|
|
stub_ci_pipeline_yaml_file('invalid: file')
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
commits = [{ message: "some message" }]
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
pipeline = service.execute(project, user,
|
|
|
|
ref: 'refs/tags/0_1',
|
|
|
|
before: '00000000',
|
|
|
|
after: '31das312',
|
|
|
|
commits: commits
|
|
|
|
)
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
expect(pipeline).to be_persisted
|
|
|
|
expect(pipeline.status).to eq("failed")
|
|
|
|
expect(pipeline.builds.any?).to be false
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
|
|
|
|
context 'when there are no jobs for this pipeline' do
|
|
|
|
before do
|
|
|
|
config = YAML.dump({ test: { script: 'ls', only: ['feature'] } })
|
|
|
|
stub_ci_pipeline_yaml_file(config)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a new pipeline' do
|
|
|
|
result = service.execute(project, user,
|
|
|
|
ref: 'refs/heads/master',
|
|
|
|
before: '00000000',
|
|
|
|
after: '31das312',
|
|
|
|
commits: [{ message: 'some msg' }])
|
|
|
|
|
|
|
|
expect(result).to be_falsey
|
|
|
|
expect(Ci::Build.all).to be_empty
|
|
|
|
expect(Ci::Pipeline.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|