Commit 66a453a5 authored by Pedro Pombeiro's avatar Pedro Pombeiro Committed by Adam Hegyi

Add 2 new sort options to runner API: CREATED_ASC and CONTACTED_DESC

parent 67529808
...@@ -24,8 +24,9 @@ module Ci ...@@ -24,8 +24,9 @@ module Ci
end end
def sort_key def sort_key
if @params[:sort] == 'contacted_asc' case @params[:sort]
'contacted_asc' when 'contacted_asc', 'contacted_desc', 'created_at_asc', 'created_at_desc'
@params[:sort]
else else
'created_date' 'created_date'
end end
......
...@@ -7,7 +7,9 @@ module Types ...@@ -7,7 +7,9 @@ module Types
description 'Values for sorting runners' description 'Values for sorting runners'
value 'CONTACTED_ASC', 'Ordered by contacted_at in ascending order.', value: :contacted_asc value 'CONTACTED_ASC', 'Ordered by contacted_at in ascending order.', value: :contacted_asc
value 'CREATED_DESC', 'Ordered by created_date in descending order.', value: :created_date value 'CONTACTED_DESC', 'Ordered by contacted_at in descending order.', value: :contacted_desc
value 'CREATED_ASC', 'Ordered by created_at in ascending order.', value: :created_at_asc
value 'CREATED_DESC', 'Ordered by created_at in descending order.', value: :created_at_desc
end end
end end
end end
...@@ -134,6 +134,8 @@ module Ci ...@@ -134,6 +134,8 @@ module Ci
end end
scope :order_contacted_at_asc, -> { order(contacted_at: :asc) } scope :order_contacted_at_asc, -> { order(contacted_at: :asc) }
scope :order_contacted_at_desc, -> { order(contacted_at: :desc) }
scope :order_created_at_asc, -> { order(created_at: :asc) }
scope :order_created_at_desc, -> { order(created_at: :desc) } scope :order_created_at_desc, -> { order(created_at: :desc) }
scope :with_tags, -> { preload(:tags) } scope :with_tags, -> { preload(:tags) }
...@@ -190,8 +192,13 @@ module Ci ...@@ -190,8 +192,13 @@ module Ci
end end
def self.order_by(order) def self.order_by(order)
if order == 'contacted_asc' case order
when 'contacted_asc'
order_contacted_at_asc order_contacted_at_asc
when 'contacted_desc'
order_contacted_at_desc
when 'created_at_asc'
order_created_at_asc
else else
order_created_at_desc order_created_at_desc
end end
......
...@@ -13841,7 +13841,9 @@ Values for sorting runners. ...@@ -13841,7 +13841,9 @@ Values for sorting runners.
| Value | Description | | Value | Description |
| ----- | ----------- | | ----- | ----------- |
| <a id="cirunnersortcontacted_asc"></a>`CONTACTED_ASC` | Ordered by contacted_at in ascending order. | | <a id="cirunnersortcontacted_asc"></a>`CONTACTED_ASC` | Ordered by contacted_at in ascending order. |
| <a id="cirunnersortcreated_desc"></a>`CREATED_DESC` | Ordered by created_date in descending order. | | <a id="cirunnersortcontacted_desc"></a>`CONTACTED_DESC` | Ordered by contacted_at in descending order. |
| <a id="cirunnersortcreated_asc"></a>`CREATED_ASC` | Ordered by created_at in ascending order. |
| <a id="cirunnersortcreated_desc"></a>`CREATED_DESC` | Ordered by created_at in descending order. |
### `CiRunnerStatus` ### `CiRunnerStatus`
......
...@@ -51,23 +51,61 @@ RSpec.describe Ci::RunnersFinder do ...@@ -51,23 +51,61 @@ RSpec.describe Ci::RunnersFinder do
end end
context 'sort' do context 'sort' do
let_it_be(:runner1) { create :ci_runner, created_at: '2018-07-12 07:00', contacted_at: 1.minute.ago }
let_it_be(:runner2) { create :ci_runner, created_at: '2018-07-12 08:00', contacted_at: 3.minutes.ago }
let_it_be(:runner3) { create :ci_runner, created_at: '2018-07-12 09:00', contacted_at: 2.minutes.ago }
subject do
described_class.new(current_user: admin, params: params).execute
end
context 'without sort param' do context 'without sort param' do
it 'sorts by created_at' do let(:params) { {} }
runner1 = create :ci_runner, created_at: '2018-07-12 07:00'
runner2 = create :ci_runner, created_at: '2018-07-12 08:00'
runner3 = create :ci_runner, created_at: '2018-07-12 09:00'
expect(described_class.new(current_user: admin, params: {}).execute).to eq [runner3, runner2, runner1] it 'sorts by created_at descending' do
is_expected.to eq [runner3, runner2, runner1]
end end
end end
context 'with sort param' do context 'with sort param equal to created_date' do
it 'sorts by specified attribute' do let(:params) { { sort: 'created_date' } }
runner1 = create :ci_runner, contacted_at: 1.minute.ago
runner2 = create :ci_runner, contacted_at: 3.minutes.ago it 'sorts by created_at descending' do
runner3 = create :ci_runner, contacted_at: 2.minutes.ago is_expected.to eq [runner3, runner2, runner1]
end
end
context 'with sort param equal to created_at_desc' do
let(:params) { { sort: 'created_at_desc' } }
it 'sorts by created_at descending' do
is_expected.to eq [runner3, runner2, runner1]
end
end
context 'with sort param equal to created_at_asc' do
let(:params) { { sort: 'created_at_asc' } }
it 'sorts by created_at ascending' do
is_expected.to eq [runner1, runner2, runner3]
end
end
context 'by contacted_at' do
context 'with sort param equal to contacted_asc' do
let(:params) { { sort: 'contacted_asc' } }
it 'sorts by contacted_at ascending' do
is_expected.to eq [runner2, runner3, runner1]
end
end
context 'with sort param equal to contacted_desc' do
let(:params) { { sort: 'contacted_desc' } }
expect(described_class.new(current_user: admin, params: { sort: 'contacted_asc' }).execute).to eq [runner2, runner3, runner1] it 'sorts by contacted_at descending' do
is_expected.to eq [runner1, runner3, runner2]
end
end end
end end
end end
......
...@@ -50,13 +50,29 @@ RSpec.describe Resolvers::Ci::RunnersResolver do ...@@ -50,13 +50,29 @@ RSpec.describe Resolvers::Ci::RunnersResolver do
it { is_expected.to eq([offline_project_runner, instance_runner, inactive_project_runner, group_runner]) } it { is_expected.to eq([offline_project_runner, instance_runner, inactive_project_runner, group_runner]) }
end end
context "set to :created_date" do context "set to :contacted_desc" do
let(:args) do let(:args) do
{ sort: :created_date } { sort: :contacted_desc }
end
it { is_expected.to eq([offline_project_runner, instance_runner, inactive_project_runner, group_runner].reverse) }
end
context "set to :created_at_desc" do
let(:args) do
{ sort: :created_at_desc }
end end
it { is_expected.to eq([instance_runner, group_runner, offline_project_runner, inactive_project_runner]) } it { is_expected.to eq([instance_runner, group_runner, offline_project_runner, inactive_project_runner]) }
end end
context "set to :created_at_asc" do
let(:args) do
{ sort: :created_at_asc }
end
it { is_expected.to eq([instance_runner, group_runner, offline_project_runner, inactive_project_runner].reverse) }
end
end end
context 'when type is filtered' do context 'when type is filtered' do
......
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