Commit e9d94451 authored by Shinya Maeda's avatar Shinya Maeda

- Add new parameters for Pipeline API

- Expand PipelinesFinder functions
parent 9fd1a35f
...@@ -9,19 +9,19 @@ class Projects::PipelinesController < Projects::ApplicationController ...@@ -9,19 +9,19 @@ class Projects::PipelinesController < Projects::ApplicationController
def index def index
@scope = params[:scope] @scope = params[:scope]
@pipelines = PipelinesFinder @pipelines = PipelinesFinder
.new(project) .new(project, scope: @scope)
.execute(scope: @scope) .execute
.page(params[:page]) .page(params[:page])
.per(30) .per(30)
@running_count = PipelinesFinder @running_count = PipelinesFinder
.new(project).execute(scope: 'running').count .new(project, scope: 'running').execute.count
@pending_count = PipelinesFinder @pending_count = PipelinesFinder
.new(project).execute(scope: 'pending').count .new(project, scope: 'pending').execute.count
@finished_count = PipelinesFinder @finished_count = PipelinesFinder
.new(project).execute(scope: 'finished').count .new(project, scope: 'finished').execute.count
@pipelines_count = PipelinesFinder @pipelines_count = PipelinesFinder
.new(project).execute.count .new(project).execute.count
......
class PipelinesFinder class PipelinesFinder
attr_reader :project, :pipelines attr_reader :project, :pipelines, :params
def initialize(project) def initialize(project, params = {})
@project = project @project = project
@pipelines = project.pipelines @pipelines = project.pipelines
@params = params
end end
def execute(scope: nil) def execute
scoped_pipelines = items = pipelines
case scope items = by_scope(items)
items = by_status(items)
items = by_ref(items)
items = by_user(items)
items = by_duration(items)
items = by_yaml_error(items)
order_and_sort(items)
end
private
def ids_for_ref(refs)
pipelines.where(ref: refs).group(:ref).select('max(id)')
end
def from_ids(ids)
pipelines.unscoped.where(id: ids)
end
def branches
project.repository.branch_names
end
def tags
project.repository.tag_names
end
def by_scope(items)
case params[:scope]
when 'running' when 'running'
pipelines.running items.running
when 'pending' when 'pending'
pipelines.pending items.pending
when 'finished' when 'finished'
pipelines.finished items.finished
when 'branches' when 'branches'
from_ids(ids_for_ref(branches)) from_ids(ids_for_ref(branches))
when 'tags' when 'tags'
from_ids(ids_for_ref(tags)) from_ids(ids_for_ref(tags))
else else
pipelines items
end
end end
scoped_pipelines.order(id: :desc) def by_status(items)
case params[:status]
when 'running'
items.running
when 'pending'
items.pending
when 'success'
items.success
when 'failed'
items.failed
when 'canceled'
items.canceled
when 'skipped'
items.skipped
else
items
end
end end
private def by_ref(items)
if params[:ref].present?
items.where(ref: params[:ref])
else
items
end
end
def ids_for_ref(refs) def by_user(items)
pipelines.where(ref: refs).group(:ref).select('max(id)') if params[:user_id].present?
items.where(user_id: params[:user_id])
else
items
end
end end
def from_ids(ids) def by_duration(items)
pipelines.unscoped.where(id: ids) if params[:duration].present?
items.where("duration > ?", params[:duration])
else
items
end
end end
def branches def by_yaml_error(items)
project.repository.branch_names if params[:yaml_error].present? && params[:yaml_error]
items.where("yaml_errors IS NOT NULL")
else
items
end
end end
def tags def order_and_sort(items)
project.repository.tag_names if params[:order_by].present? && params[:sort].present?
items.order("#{params[:order_by]} #{params[:sort]}")
else
items.order(id: :desc)
end
end end
end end
...@@ -16,11 +16,21 @@ module API ...@@ -16,11 +16,21 @@ module API
use :pagination use :pagination
optional :scope, type: String, values: %w(running branches tags), optional :scope, type: String, values: %w(running branches tags),
desc: 'Either running, branches, or tags' desc: 'Either running, branches, or tags'
optional :status, type: String, values: ['running', 'pending', 'success', 'failed', 'canceled', 'skipped'],
desc: 'Pipeline Status'
optional :ref, type: String, desc: 'Pipeline Ref'
optional :duration, type: Integer, desc: 'Greater than the specified duration'
optional :yaml_error, type: Boolean, desc: 'If true, returns only yaml error pipelines.'
optional :user_id, type: String, desc: 'User who executed pipelines'
optional :order_by, type: String, values: ['id', 'status', 'ref', 'user_id', 'started_at', 'finished_at', 'created_at', 'updated_at'], default: 'id',
desc: 'Return issues ordered by `created_at` or `updated_at` fields.'
optional :sort, type: String, values: ['asc', 'desc'], default: 'desc',
desc: 'Return pipelines sorted in `asc` or `desc` order.'
end end
get ':id/pipelines' do get ':id/pipelines' do
authorize! :read_pipeline, user_project authorize! :read_pipeline, user_project
pipelines = PipelinesFinder.new(user_project).execute(scope: params[:scope]) pipelines = PipelinesFinder.new(user_project, params).execute(scope: params[:scope])
present paginate(pipelines), with: Entities::PipelineBasic present paginate(pipelines), with: Entities::PipelineBasic
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment