summaryrefslogtreecommitdiff
path: root/app/finders
diff options
context:
space:
mode:
authorKamil Trzcinski <ayufan@ayufan.eu>2016-05-10 02:26:13 +0300
committerKamil Trzcinski <ayufan@ayufan.eu>2016-05-10 02:26:13 +0300
commitfe2137d871b978033007a3375cf6f1edf0f04cd4 (patch)
treee75b2aca3150efd6433314fb57e10c6ba763db6e /app/finders
parent504a1fac95a2af2cf90f00f310d244d8d37f0015 (diff)
downloadgitlab-ce-fe2137d871b978033007a3375cf6f1edf0f04cd4.tar.gz
Improve pipelines design
Diffstat (limited to 'app/finders')
-rw-r--r--app/finders/pipelines_finder.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/app/finders/pipelines_finder.rb b/app/finders/pipelines_finder.rb
new file mode 100644
index 00000000000..c19a795d467
--- /dev/null
+++ b/app/finders/pipelines_finder.rb
@@ -0,0 +1,38 @@
+class PipelinesFinder
+ attr_reader :project
+
+ def initialize(project)
+ @project = project
+ end
+
+ def execute(pipelines, scope)
+ case scope
+ when 'running'
+ pipelines.running_or_pending
+ when 'branches'
+ from_ids(pipelines, ids_for_ref(pipelines, branches))
+ when 'tags'
+ from_ids(pipelines, ids_for_ref(pipelines, tags))
+ else
+ pipelines
+ end
+ end
+
+ private
+
+ def ids_for_ref(pipelines, refs)
+ pipelines.where(ref: refs).group(:ref).select('max(id)')
+ end
+
+ def from_ids(pipelines, ids)
+ pipelines.unscoped.where(id: ids)
+ end
+
+ def branches
+ project.repository.branches.map(&:name)
+ end
+
+ def tags
+ project.repository.tags.map(&:name)
+ end
+end