Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
86179076
Commit
86179076
authored
Feb 06, 2019
by
GitLab Bot
Browse files
Options
Browse Files
Download
Plain Diff
Automatic merge of gitlab-org/gitlab-ce master
parents
40dc49b8
561ce0c0
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
177 additions
and
0 deletions
+177
-0
app/controllers/projects/environments_controller.rb
app/controllers/projects/environments_controller.rb
+16
-0
app/models/environment.rb
app/models/environment.rb
+12
-0
changelogs/unreleased/introduce-environment-search-endpoint.yml
...logs/unreleased/introduce-environment-search-endpoint.yml
+5
-0
config/routes/project.rb
config/routes/project.rb
+1
-0
spec/controllers/projects/environments_controller_spec.rb
spec/controllers/projects/environments_controller_spec.rb
+73
-0
spec/models/environment_spec.rb
spec/models/environment_spec.rb
+70
-0
No files found.
app/controllers/projects/environments_controller.rb
View file @
86179076
...
...
@@ -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
)
...
...
app/models/environment.rb
View file @
86179076
...
...
@@ -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
)
...
...
changelogs/unreleased/introduce-environment-search-endpoint.yml
0 → 100644
View file @
86179076
---
title
:
Introduce Internal API for searching environment names
merge_request
:
24923
author
:
type
:
added
config/routes/project.rb
View file @
86179076
...
...
@@ -277,6 +277,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
collection
do
get
:metrics
,
action: :metrics_redirect
get
:folder
,
path:
'folders/*id'
,
constraints:
{
format:
/(html|json)/
}
get
:search
end
resources
:deployments
,
only:
[
:index
]
do
...
...
spec/controllers/projects/environments_controller_spec.rb
View file @
86179076
...
...
@@ -422,6 +422,79 @@ describe Projects::EnvironmentsController do
end
end
describe
'GET #search'
do
before
do
create
(
:environment
,
name:
'staging'
,
project:
project
)
create
(
:environment
,
name:
'review/patch-1'
,
project:
project
)
create
(
:environment
,
name:
'review/patch-2'
,
project:
project
)
end
let
(
:query
)
{
'pro'
}
it
'responds with status code 200'
do
get
:search
,
params:
environment_params
(
format: :json
,
query:
query
)
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
end
it
'returns matched results'
do
get
:search
,
params:
environment_params
(
format: :json
,
query:
query
)
expect
(
json_response
).
to
contain_exactly
(
'production'
)
end
context
'when query is review'
do
let
(
:query
)
{
'review'
}
it
'returns matched results'
do
get
:search
,
params:
environment_params
(
format: :json
,
query:
query
)
expect
(
json_response
).
to
contain_exactly
(
'review/patch-1'
,
'review/patch-2'
)
end
end
context
'when query is empty'
do
let
(
:query
)
{
''
}
it
'returns matched results'
do
get
:search
,
params:
environment_params
(
format: :json
,
query:
query
)
expect
(
json_response
)
.
to
contain_exactly
(
'production'
,
'staging'
,
'review/patch-1'
,
'review/patch-2'
)
end
end
context
'when query is review/patch-3'
do
let
(
:query
)
{
'review/patch-3'
}
it
'responds with status code 204'
do
get
:search
,
params:
environment_params
(
format: :json
,
query:
query
)
expect
(
response
).
to
have_gitlab_http_status
(
:no_content
)
end
end
context
'when query is partially matched in the middle of environment name'
do
let
(
:query
)
{
'patch'
}
it
'responds with status code 204'
do
get
:search
,
params:
environment_params
(
format: :json
,
query:
query
)
expect
(
response
).
to
have_gitlab_http_status
(
:no_content
)
end
end
context
'when query contains a wildcard character'
do
let
(
:query
)
{
'review%'
}
it
'prevents wildcard injection'
do
get
:search
,
params:
environment_params
(
format: :json
,
query:
query
)
expect
(
response
).
to
have_gitlab_http_status
(
:no_content
)
end
end
end
def
environment_params
(
opts
=
{})
opts
.
reverse_merge
(
namespace_id:
project
.
namespace
,
project_id:
project
,
...
...
spec/models/environment_spec.rb
View file @
86179076
...
...
@@ -41,6 +41,76 @@ describe Environment do
end
end
describe
'.for_name_like'
do
subject
{
project
.
environments
.
for_name_like
(
query
,
limit:
limit
)
}
let!
(
:environment
)
{
create
(
:environment
,
name:
'production'
,
project:
project
)
}
let
(
:query
)
{
'pro'
}
let
(
:limit
)
{
5
}
it
'returns a found name'
do
is_expected
.
to
include
(
environment
)
end
context
'when query is production'
do
let
(
:query
)
{
'production'
}
it
'returns a found name'
do
is_expected
.
to
include
(
environment
)
end
end
context
'when query is productionA'
do
let
(
:query
)
{
'productionA'
}
it
'returns empty array'
do
is_expected
.
to
be_empty
end
end
context
'when query is empty'
do
let
(
:query
)
{
''
}
it
'returns a found name'
do
is_expected
.
to
include
(
environment
)
end
end
context
'when query is nil'
do
let
(
:query
)
{
}
it
'raises an error'
do
expect
{
subject
}.
to
raise_error
(
NoMethodError
)
end
end
context
'when query is partially matched in the middle of environment name'
do
let
(
:query
)
{
'duction'
}
it
'returns empty array'
do
is_expected
.
to
be_empty
end
end
context
'when query contains a wildcard character'
do
let
(
:query
)
{
'produc%'
}
it
'prevents wildcard injection'
do
is_expected
.
to
be_empty
end
end
end
describe
'.pluck_names'
do
subject
{
described_class
.
pluck_names
}
let!
(
:environment
)
{
create
(
:environment
,
name:
'production'
,
project:
project
)
}
it
'plucks names'
do
is_expected
.
to
eq
(
%w[production]
)
end
end
describe
'#expire_etag_cache'
do
let
(
:store
)
{
Gitlab
::
EtagCaching
::
Store
.
new
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment