summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDylan Griffith <dyl.griffith@gmail.com>2018-02-20 16:11:57 +1100
committerDylan Griffith <dyl.griffith@gmail.com>2018-02-23 17:22:16 +1100
commit76d14eca332d6e0a1928cd40cf4d9f126e46caf7 (patch)
tree426c8d174e4164988032ae94cb59ce1680e1c62a
parent557db7e635c70bf68a15f7029014301013b30070 (diff)
downloadgitlab-ce-43319-invalid-environment-not-being-created-should-be-surfaced-to-user.tar.gz
Don't create pipeline when environment creation fails (closes #43319)43319-invalid-environment-not-being-created-should-be-surfaced-to-user
-rw-r--r--app/models/commit_status.rb3
-rw-r--r--app/services/ci/create_build_environment_service.rb8
-rw-r--r--app/services/ci/create_pipeline_stages_service.rb7
-rw-r--r--changelogs/unreleased/43319-invalid-environment-not-being-created-should-be-surfaced-to-user.yml5
-rw-r--r--spec/services/ci/create_build_environment_service_spec.rb29
5 files changed, 47 insertions, 5 deletions
diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb
index 3469d5d795c..ca4df28bbb9 100644
--- a/app/models/commit_status.rb
+++ b/app/models/commit_status.rb
@@ -44,7 +44,8 @@ class CommitStatus < ActiveRecord::Base
api_failure: 2,
stuck_or_timeout_failure: 3,
runner_system_failure: 4,
- missing_dependency_failure: 5
+ missing_dependency_failure: 5,
+ environment_creation_error: 6
}
##
diff --git a/app/services/ci/create_build_environment_service.rb b/app/services/ci/create_build_environment_service.rb
new file mode 100644
index 00000000000..c7c022e76ab
--- /dev/null
+++ b/app/services/ci/create_build_environment_service.rb
@@ -0,0 +1,8 @@
+module Ci
+ class CreateBuildEnvironmentService < BaseService
+ def execute(build)
+ environment_name = build.expanded_environment_name
+ project.environments.find_or_create_by!(name: environment_name)
+ end
+ end
+end
diff --git a/app/services/ci/create_pipeline_stages_service.rb b/app/services/ci/create_pipeline_stages_service.rb
index f2c175adee6..7b13f8b3f1d 100644
--- a/app/services/ci/create_pipeline_stages_service.rb
+++ b/app/services/ci/create_pipeline_stages_service.rb
@@ -6,12 +6,11 @@ module Ci
seed.create! do |build|
##
- # Create the environment before the build starts. This sets its slug and
- # makes it available as an environment variable
+ # Create the environment before the build starts. This sets its
+ # slug and makes it available as an environment variable
#
if build.has_environment?
- environment_name = build.expanded_environment_name
- project.environments.find_or_create_by(name: environment_name)
+ CreateBuildEnvironmentService.new(project, current_user).execute(build)
end
end
end
diff --git a/changelogs/unreleased/43319-invalid-environment-not-being-created-should-be-surfaced-to-user.yml b/changelogs/unreleased/43319-invalid-environment-not-being-created-should-be-surfaced-to-user.yml
new file mode 100644
index 00000000000..e97f4e280fe
--- /dev/null
+++ b/changelogs/unreleased/43319-invalid-environment-not-being-created-should-be-surfaced-to-user.yml
@@ -0,0 +1,5 @@
+---
+title: Don't create pipeline when environment creation fails
+merge_request: 17202
+author:
+type: fixed
diff --git a/spec/services/ci/create_build_environment_service_spec.rb b/spec/services/ci/create_build_environment_service_spec.rb
new file mode 100644
index 00000000000..99f4c17ac16
--- /dev/null
+++ b/spec/services/ci/create_build_environment_service_spec.rb
@@ -0,0 +1,29 @@
+require 'spec_helper'
+
+describe Ci::CreateBuildEnvironmentService do
+ let(:service) { described_class.new(project, user) }
+ let(:job) { build(:ci_build, environment: environment_name) }
+ let(:project) { job.pipeline.project }
+ let(:user) { build(:user) }
+ let(:environment_name) { 'the-environment-name' }
+
+ describe '#execute' do
+ it 'creates the environment' do
+ service.execute(job)
+
+ expect(project.environments.where(name: 'the-environment-name'))
+ .not_to be_empty
+ end
+
+ context 'when the environment fails to be created' do
+ let(:environment_name) { '/invalid-environment-name' }
+ it 'raises ActiveRecord::RecordInvalid' do
+ expect { service.execute(job) }
+ .to raise_error(
+ ActiveRecord::RecordInvalid,
+ a_string_including("cannot start or end with '/'")
+ )
+ end
+ end
+ end
+end