From 4b6619cfd3ca127d728d7277cac3da8ed54b99b0 Mon Sep 17 00:00:00 2001 From: Alexis Reigel Date: Mon, 11 Jun 2018 13:37:47 +0200 Subject: add type param to runners api --- app/models/ci/runner.rb | 27 ++++++++++++----------- doc/api/runners.md | 4 ++++ lib/api/runners.rb | 22 ++++++++++++++++--- spec/requests/api/runners_spec.rb | 45 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 16 deletions(-) diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb index eabb41c29d7..043f03b7873 100644 --- a/app/models/ci/runner.rb +++ b/app/models/ci/runner.rb @@ -8,12 +8,24 @@ module Ci include RedisCacheable include ChronicDurationAttribute + enum access_level: { + not_protected: 0, + ref_protected: 1 + } + + enum runner_type: { + instance_type: 1, + group_type: 2, + project_type: 3 + } + RUNNER_QUEUE_EXPIRY_TIME = 60.minutes ONLINE_CONTACT_TIMEOUT = 1.hour UPDATE_DB_RUNNER_INFO_EVERY = 40.minutes - AVAILABLE_TYPES = %w[specific shared].freeze + AVAILABLE_TYPES_LEGACY = %w[specific shared].freeze + AVAILABLE_TYPES = runner_types.keys.freeze AVAILABLE_STATUSES = %w[active paused online offline].freeze - AVAILABLE_SCOPES = (AVAILABLE_TYPES + AVAILABLE_STATUSES).freeze + AVAILABLE_SCOPES = (AVAILABLE_TYPES_LEGACY + AVAILABLE_TYPES + AVAILABLE_STATUSES).freeze FORM_EDITABLE = %i[description tag_list active run_untagged locked access_level maximum_timeout_human_readable].freeze ignore_column :is_shared @@ -88,17 +100,6 @@ module Ci after_destroy :cleanup_runner_queue - enum access_level: { - not_protected: 0, - ref_protected: 1 - } - - enum runner_type: { - instance_type: 1, - group_type: 2, - project_type: 3 - } - cached_attr_reader :version, :revision, :platform, :architecture, :ip_address, :contacted_at chronic_duration_attr :maximum_timeout_human_readable, :maximum_timeout diff --git a/doc/api/runners.md b/doc/api/runners.md index 66476e7db64..7763e6b2913 100644 --- a/doc/api/runners.md +++ b/doc/api/runners.md @@ -11,11 +11,13 @@ Get a list of specific runners available to the user. ``` GET /runners GET /runners?scope=active +GET /runners?type=project_type ``` | Attribute | Type | Required | Description | |-----------|---------|----------|---------------------| | `scope` | string | no | The scope of specific runners to show, one of: `active`, `paused`, `online`, `offline`; showing all runners if none provided | +| `type` | string | no | The type of runners to show, one of: `instance_type`, `group_type`, `project_type` | ``` curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v4/runners" @@ -56,11 +58,13 @@ is restricted to users with `admin` privileges. ``` GET /runners/all GET /runners/all?scope=online +GET /runners/all?type=project_type ``` | Attribute | Type | Required | Description | |-----------|---------|----------|---------------------| | `scope` | string | no | The scope of runners to show, one of: `specific`, `shared`, `active`, `paused`, `online`, `offline`; showing all runners if none provided | +| `type` | string | no | The type of runners to show, one of: `instance_type`, `group_type`, `project_type` | ``` curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v4/runners/all" diff --git a/lib/api/runners.rb b/lib/api/runners.rb index 30abd0b63e9..d24ff8641e1 100644 --- a/lib/api/runners.rb +++ b/lib/api/runners.rb @@ -11,10 +11,15 @@ module API params do optional :scope, type: String, values: Ci::Runner::AVAILABLE_STATUSES, desc: 'The scope of specific runners to show' + optional :type, type: String, values: Ci::Runner::AVAILABLE_TYPES, + desc: 'The type of the runners to show' use :pagination end get do - runners = filter_runners(current_user.ci_owned_runners, params[:scope], allowed_scopes: Ci::Runner::AVAILABLE_STATUSES) + runners = current_user.ci_owned_runners + runners = filter_runners(runners, params[:scope], allowed_scopes: Ci::Runner::AVAILABLE_STATUSES) + runners = filter_runners(runners, params[:type], allowed_scopes: Ci::Runner::AVAILABLE_TYPES) + present paginate(runners), with: Entities::Runner end @@ -24,11 +29,17 @@ module API params do optional :scope, type: String, values: Ci::Runner::AVAILABLE_SCOPES, desc: 'The scope of specific runners to show' + optional :type, type: String, values: Ci::Runner::AVAILABLE_TYPES, + desc: 'The type of the runners to show' use :pagination end get 'all' do authenticated_as_admin! - runners = filter_runners(Ci::Runner.all, params[:scope]) + + runners = Ci::Runner.all + runners = filter_runners(runners, params[:scope]) + runners = filter_runners(runners, params[:type], allowed_scopes: Ci::Runner::AVAILABLE_TYPES) + present paginate(runners), with: Entities::Runner end @@ -116,10 +127,15 @@ module API params do optional :scope, type: String, values: Ci::Runner::AVAILABLE_SCOPES, desc: 'The scope of specific runners to show' + optional :type, type: String, values: Ci::Runner::AVAILABLE_TYPES, + desc: 'The type of the runners to show' use :pagination end get ':id/runners' do - runners = filter_runners(Ci::Runner.owned_or_instance_wide(user_project.id), params[:scope]) + runners = Ci::Runner.owned_or_instance_wide(user_project.id) + runners = filter_runners(runners, params[:scope]) + runners = filter_runners(runners, params[:type], allowed_scopes: Ci::Runner::AVAILABLE_TYPES) + present paginate(runners), with: Entities::Runner end diff --git a/spec/requests/api/runners_spec.rb b/spec/requests/api/runners_spec.rb index c2fed09a5d2..3939500d7ca 100644 --- a/spec/requests/api/runners_spec.rb +++ b/spec/requests/api/runners_spec.rb @@ -55,6 +55,21 @@ describe API::Runners do get api('/runners?scope=unknown', user) expect(response).to have_gitlab_http_status(400) end + + it 'filters runners by type' do + get api('/runners?type=project_type', user) + + expect(json_response).to match_array [ + a_hash_including('description' => 'Project runner'), + a_hash_including('description' => 'Two projects runner') + ] + end + + it 'does not filter by invalid type' do + get api('/runners?type=bogus', user) + + expect(response).to have_gitlab_http_status(400) + end end context 'unauthorized user' do @@ -115,6 +130,21 @@ describe API::Runners do get api('/runners/all?scope=unknown', admin) expect(response).to have_gitlab_http_status(400) end + + it 'filters runners by type' do + get api('/runners/all?type=project_type', admin) + + expect(json_response).to match_array [ + a_hash_including('description' => 'Project runner'), + a_hash_including('description' => 'Two projects runner') + ] + end + + it 'does not filter by invalid type' do + get api('/runners/all?type=bogus', admin) + + expect(response).to have_gitlab_http_status(400) + end end context 'unauthorized user' do @@ -603,6 +633,21 @@ describe API::Runners do get api("/projects/#{project.id}/runners?scope=unknown", user) expect(response).to have_gitlab_http_status(400) end + + it 'filters runners by type' do + get api("/projects/#{project.id}/runners?type=project_type", user) + + expect(json_response).to match_array [ + a_hash_including('description' => 'Project runner'), + a_hash_including('description' => 'Two projects runner') + ] + end + + it 'does not filter by invalid type' do + get api("/projects/#{project.id}/runners?type=bogus", user) + + expect(response).to have_gitlab_http_status(400) + end end context 'authorized user without maintainer privileges' do -- cgit v1.2.1