Add name(User)

This commit is contained in:
Shinya Maeda 2017-03-08 20:24:00 +09:00
parent 83d02a0b60
commit 175800299b
4 changed files with 36 additions and 8 deletions

View File

@ -79,6 +79,14 @@ class PipelinesFinder
end
end
def by_name(items)
if params[:name].present?
items.joins(:user).where("users.name = ?", params[:name])
else
items
end
end
def by_username(items)
if params[:username].present?
items.joins(:user).where("users.username = ?", params[:username])

View File

@ -15,7 +15,8 @@ GET /projects/:id/pipelines
| `status` | string | no | The status of pipelines, one of: `running`, `pending`, `success`, `failed`, `canceled`, `skipped`; |
| `ref` | string | no | The ref of pipelines |
| `yaml_errors`| string | no | If true, returns only yaml error pipelines |
| `username`| string | no | The name of user who triggered pipelines |
| `name`| string | no | The name of user who triggered pipelines |
| `username`| string | no | The username of user who triggered pipelines |
| `order_by`| string | no | Return requests ordered by `id`, `status`, `ref`, `username`, `started_at`, `finished_at`, `created_at` or `updated_at` fields. Default is `id` |
| `sort` | string | no | Return requests sorted in `asc` or `desc` order. Default is `desc` |

View File

@ -20,7 +20,8 @@ module API
desc: 'The status of pipelines'
optional :ref, type: String, desc: 'The ref of pipelines'
optional :yaml_errors, type: Boolean, desc: 'If true, returns only yaml error pipelines'
optional :username, type: String, desc: 'The name of user who triggered pipelines'
optional :name, type: String, desc: 'The name of user who triggered pipelines'
optional :username, type: String, desc: 'The username of user who triggered pipelines'
optional :order_by, type: String, values: %w[id status ref username started_at finished_at created_at updated_at], default: 'id',
desc: 'The order_by which is combined with a sort'
optional :sort, type: String, values: %w[asc desc], default: 'desc',

View File

@ -134,7 +134,25 @@ describe PipelinesFinder do
end
context 'when a ref does not exist' do
let(:params) { { ref: 'unique-ref' } }
let(:params) { { ref: 'invalid-ref' } }
it 'selects nothing' do
expect(subject).to be_empty
end
end
end
context 'when a name is passed' do
context 'when a name exists' do
let(:params) { { name: user1.name } }
it 'selects all pipelines which belong to the name' do
expect(subject).to match_array(Ci::Pipeline.where(user: user1))
end
end
context 'when a name does not exist' do
let(:params) { { name: 'invalid-name' } }
it 'selects nothing' do
expect(subject).to be_empty
@ -152,7 +170,7 @@ describe PipelinesFinder do
end
context 'when a username does not exist' do
let(:params) { { username: 'unique-username' } }
let(:params) { { username: 'invalid-username' } }
it 'selects nothing' do
expect(subject).to be_empty
@ -204,7 +222,7 @@ describe PipelinesFinder do
end
context 'when order_by does not exist' do
let(:params) { { order_by: 'abnormal_column', sort: 'desc' } }
let(:params) { { order_by: 'invalid_column', sort: 'desc' } }
it 'sorts by default' do
expect(subject).to match_array(Ci::Pipeline.order(id: :desc))
@ -212,7 +230,7 @@ describe PipelinesFinder do
end
context 'when sort does not exist' do
let(:params) { { order_by: 'created_at', sort: 'abnormal_sort' } }
let(:params) { { order_by: 'created_at', sort: 'invalid_sort' } }
it 'sorts by default' do
expect(subject).to match_array(Ci::Pipeline.order(id: :desc))