diff options
author | Shinya Maeda <shinya@gitlab.com> | 2019-02-05 10:14:30 +0300 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2019-02-06 12:14:18 +0300 |
commit | e8d9df83a6f5638b3207885220b9441b7b894e46 (patch) | |
tree | d28aa89c50dd1916721d2aad562fb95249d40f75 /app | |
parent | 591380a3f1bf1b5220f176f082af297831a1886e (diff) |
Inroduce Internal API for searching environment names
Add changelog
Rename word to query
User hash for limit
Do not allow control limit
Rename pluck names and add more specs
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/projects/environments_controller.rb | 16 | ||||
-rw-r--r-- | app/models/environment.rb | 12 |
2 files changed, 28 insertions, 0 deletions
diff --git a/app/controllers/projects/environments_controller.rb b/app/controllers/projects/environments_controller.rb index 4e85de25c6b..79685e8b675 100644 --- a/app/controllers/projects/environments_controller.rb +++ b/app/controllers/projects/environments_controller.rb @@ -158,6 +158,16 @@ class Projects::EnvironmentsController < Projects::ApplicationController end end + def search + respond_to do |format| + format.json do + environment_names = search_environment_names + + render json: environment_names, status: environment_names.any? ? :ok : :no_content + end + end + end + private def verify_api_request! @@ -181,6 +191,12 @@ class Projects::EnvironmentsController < Projects::ApplicationController @environment ||= project.environments.find(params[:id]) end + def search_environment_names + return [] unless params[:query] + + project.environments.for_name_like(params[:query]).pluck_names + end + def serialize_environments(request, response, nested = false) EnvironmentSerializer .new(project: @project, current_user: @current_user) diff --git a/app/models/environment.rb b/app/models/environment.rb index cdfe3b7c023..1fc088b12ae 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -50,6 +50,14 @@ class Environment < ActiveRecord::Base end scope :in_review_folder, -> { where(environment_type: "review") } scope :for_name, -> (name) { where(name: name) } + + ## + # Search environments which have names like the given query. + # Do not set a large limit unless you've confirmed that it works on gitlab.com scale. + scope :for_name_like, -> (query, limit: 5) do + where('name LIKE ?', "#{sanitize_sql_like(query)}%").limit(limit) + end + scope :for_project, -> (project) { where(project_id: project) } scope :with_deployment, -> (sha) { where('EXISTS (?)', Deployment.select(1).where('deployments.environment_id = environments.id').where(sha: sha)) } @@ -70,6 +78,10 @@ class Environment < ActiveRecord::Base end end + def self.pluck_names + pluck(:name) + end + def predefined_variables Gitlab::Ci::Variables::Collection.new .append(key: 'CI_ENVIRONMENT_NAME', value: name) |