summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLee Tickett <lee@tickett.net>2019-06-24 12:18:40 +0000
committerKamil TrzciƄski <ayufan@ayufan.eu>2019-06-24 12:18:40 +0000
commitec66f1b5ca20764abe21524792480755b614dbc7 (patch)
tree3ec0bcd669e9d015687effe037c18a3acf389952
parentcf137da316175ed43a980f57e22a81c537a54169 (diff)
downloadgitlab-ce-ec66f1b5ca20764abe21524792480755b614dbc7.tar.gz
Add name & search parameters to project environments API
-rw-r--r--app/finders/environments_finder.rb29
-rw-r--r--changelogs/add-name-parameter-to-project-environments-api.yml5
-rw-r--r--doc/api/environments.md4
-rw-r--r--lib/api/environments.rb7
-rw-r--r--spec/requests/api/environments_spec.rb41
5 files changed, 84 insertions, 2 deletions
diff --git a/app/finders/environments_finder.rb b/app/finders/environments_finder.rb
index 419be46fafe..29c00e4b2c2 100644
--- a/app/finders/environments_finder.rb
+++ b/app/finders/environments_finder.rb
@@ -47,6 +47,19 @@ class EnvironmentsFinder
end
# rubocop: enable CodeReuse/ActiveRecord
+ # This method will eventually take the place of `#execute` as an
+ # efficient way to get relevant environment entries.
+ # Currently, `#execute` method has a serious technical debt and
+ # we will likely rework on it in the future.
+ # See more https://gitlab.com/gitlab-org/gitlab-ce/issues/63381
+ def find
+ environments = project.environments
+ environments = by_name(environments)
+ environments = by_search(environments)
+
+ environments
+ end
+
private
def ref
@@ -56,4 +69,20 @@ class EnvironmentsFinder
def commit
params[:commit]
end
+
+ def by_name(environments)
+ if params[:name].present?
+ environments.for_name(params[:name])
+ else
+ environments
+ end
+ end
+
+ def by_search(environments)
+ if params[:search].present?
+ environments.for_name_like(params[:search], limit: nil)
+ else
+ environments
+ end
+ end
end
diff --git a/changelogs/add-name-parameter-to-project-environments-api.yml b/changelogs/add-name-parameter-to-project-environments-api.yml
new file mode 100644
index 00000000000..01d456eb75c
--- /dev/null
+++ b/changelogs/add-name-parameter-to-project-environments-api.yml
@@ -0,0 +1,5 @@
+---
+title: Add `name` and `search` parameters to project environments API
+merge_request: 29385
+author: Lee Tickett
+type: added
diff --git a/doc/api/environments.md b/doc/api/environments.md
index ebcdc546d08..44f86861ff7 100644
--- a/doc/api/environments.md
+++ b/doc/api/environments.md
@@ -11,9 +11,11 @@ GET /projects/:id/environments
| Attribute | Type | Required | Description |
| --------- | ------- | -------- | --------------------- |
| `id` | integer/string | yes | The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user |
+| `name` | string | no | Return the environment with this name. Mutually exclusive with `search` |
+| `search` | string | no | Return list of environments matching the search criteria. Mutually exclusive with `name` |
```bash
-curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/projects/1/environments
+curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/projects/1/environments?name=review%2Ffix-foo
```
Example response:
diff --git a/lib/api/environments.rb b/lib/api/environments.rb
index 6cd43923559..ec58b3b7bb9 100644
--- a/lib/api/environments.rb
+++ b/lib/api/environments.rb
@@ -18,11 +18,16 @@ module API
end
params do
use :pagination
+ optional :name, type: String, desc: 'Returns the environment with this name'
+ optional :search, type: String, desc: 'Returns list of environments matching the search criteria'
+ mutually_exclusive :name, :search, message: 'cannot be used together'
end
get ':id/environments' do
authorize! :read_environment, user_project
- present paginate(user_project.environments), with: Entities::Environment, current_user: current_user
+ environments = ::EnvironmentsFinder.new(user_project, current_user, params).find
+
+ present paginate(environments), with: Entities::Environment, current_user: current_user
end
desc 'Creates a new environment' do
diff --git a/spec/requests/api/environments_spec.rb b/spec/requests/api/environments_spec.rb
index 8fc7fdc8632..745f3c55ac8 100644
--- a/spec/requests/api/environments_spec.rb
+++ b/spec/requests/api/environments_spec.rb
@@ -34,6 +34,47 @@ describe API::Environments do
expect(json_response.first['project'].keys).to contain_exactly(*project_data_keys)
expect(json_response.first).not_to have_key("last_deployment")
end
+
+ context 'when filtering' do
+ let!(:environment2) { create(:environment, project: project) }
+
+ it 'returns environment by name' do
+ get api("/projects/#{project.id}/environments?name=#{environment.name}", user)
+
+ expect(response).to have_gitlab_http_status(200)
+ expect(response).to include_pagination_headers
+ expect(json_response).to be_an Array
+ expect(json_response.size).to eq(1)
+ expect(json_response.first['name']).to eq(environment.name)
+ end
+
+ it 'returns no environment by non-existent name' do
+ get api("/projects/#{project.id}/environments?name=test", user)
+
+ expect(response).to have_gitlab_http_status(200)
+ expect(response).to include_pagination_headers
+ expect(json_response).to be_an Array
+ expect(json_response.size).to eq(0)
+ end
+
+ it 'returns environments by name_like' do
+ get api("/projects/#{project.id}/environments?search=envir", user)
+
+ expect(response).to have_gitlab_http_status(200)
+ expect(response).to include_pagination_headers
+ expect(json_response).to be_an Array
+ expect(json_response.size).to eq(2)
+ end
+
+ it 'returns no environment by non-existent name_like' do
+ get api("/projects/#{project.id}/environments?search=test", user)
+
+ expect(response).to have_gitlab_http_status(200)
+ expect(response).to include_pagination_headers
+ expect(json_response).to be_an Array
+ expect(json_response.size).to eq(0)
+ end
+ end
end
context 'as non member' do