summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-11-25 11:15:56 +0100
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-01-02 13:49:09 +0100
commit194bc72996236297a3ccbc42bb2728e3817c1c06 (patch)
tree1d77747d7592f8daa53e2247325f8e04a1f00f76
parentf960cca54b44d7384dd74086c0898c5d5a1ff228 (diff)
downloadgitlab-ce-194bc72996236297a3ccbc42bb2728e3817c1c06.tar.gz
Add pipelines scope to select pipelines with builds
-rw-r--r--app/models/ci/pipeline.rb4
-rw-r--r--app/services/ci/create_pipeline_builds_service.rb14
-rw-r--r--spec/models/ci/pipeline_spec.rb39
3 files changed, 50 insertions, 7 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index abbbddaa4f6..d69a9dabcfc 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -21,6 +21,10 @@ module Ci
after_create :keep_around_commits, unless: :importing?
+ scope :with_builds, -> do
+ joins(:builds).merge(Ci::Build.all)
+ end
+
state_machine :status, initial: :created do
event :enqueue do
transition created: :pending
diff --git a/app/services/ci/create_pipeline_builds_service.rb b/app/services/ci/create_pipeline_builds_service.rb
index b7da3f8e7eb..2617d99e821 100644
--- a/app/services/ci/create_pipeline_builds_service.rb
+++ b/app/services/ci/create_pipeline_builds_service.rb
@@ -25,14 +25,14 @@ module Ci
user: current_user,
trigger_request: trigger_request
)
- build = pipeline.builds.create(build_attributes)
- # Create the environment before the build starts. This sets its slug and
- # makes it available as an environment variable
- project.environments.find_or_create_by(name: build.expanded_environment_name) if
- build.has_environment?
-
- build
+ pipeline.builds.create(build_attributes).tap do |build|
+ # Create the environment before the build starts. This sets its slug and
+ # makes it available as an environment variable
+ if build.has_environment?
+ project.environments.find_or_create_by(name: build.expanded_environment_name)
+ end
+ end
end
def new_builds
diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb
index d463a652bea..ea5602ca84f 100644
--- a/spec/models/ci/pipeline_spec.rb
+++ b/spec/models/ci/pipeline_spec.rb
@@ -1008,5 +1008,44 @@ describe Ci::Pipeline, :models do
expect(described_class.unfinished.count).to eq 3
end
end
+
+ describe '.with_builds' do
+ context 'when pipeline has builds' do
+ before do
+ create(:ci_build, pipeline: pipeline)
+ end
+
+ it 'finds the pipeline with builds' do
+ expect(described_class.with_builds).to include pipeline
+ end
+ end
+
+ context 'when pipeline has only external statuses' do
+ before do
+ create(:generic_commit_status, pipeline: pipeline)
+ end
+
+ it 'does not find without builds' do
+ expect(described_class.with_builds).to be_empty
+ end
+ end
+
+ context 'when pipeline has builds and external statuses' do
+ before do
+ create(:ci_build, pipeline: pipeline)
+ create(:generic_commit_status, pipeline: pipeline)
+ end
+
+ it 'finds pipeline with build' do
+ expect(described_class.with_builds).to include pipeline
+ end
+ end
+
+ context 'when pipeline is empty' do
+ it 'does not find it' do
+ expect(described_class.with_builds).to be_empty
+ end
+ end
+ end
end
end