Commit a9f10260 authored by Shinya Maeda's avatar Shinya Maeda

Merge branch '34051-use-same-endpoint-for-html-and-json-in-operationscontroller' into 'master'

Resolve "Use Same Endpoint for HTML and JSON in OperationsController"

See merge request gitlab-org/gitlab!67112
parents 265605b4 6d7078e7
# frozen_string_literal: true # frozen_string_literal: true
# Note: Both Operations dashboard (https://docs.gitlab.com/ee/user/operations_dashboard/) and Environments dashboard (https://docs.gitlab.com/ee/ci/environments/environments_dashboard.html) features are co-existing in the same controller.
class OperationsController < ApplicationController class OperationsController < ApplicationController
before_action :authorize_read_operations_dashboard! before_action :authorize_read_operations_dashboard!
respond_to :json, only: [:list]
feature_category :release_orchestration feature_category :release_orchestration
POLLING_INTERVAL = 120_000 POLLING_INTERVAL = 120_000
# Used by Operations dashboard.
def index def index
end respond_to do |format|
format.html
def environments
end
def list format.json do
Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL) set_polling_interval_header
projects = load_projects projects = load_projects
render json: { projects: serialize_as_json(projects) } render json: { projects: serialize_as_json(projects) }
end end
end
end
def environments_list # Used by Environments dashboard.
Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL) def environments
respond_to do |format|
format.html
format.json do
set_polling_interval_header
projects = load_environments_projects projects = load_environments_projects
render json: { projects: serialize_as_json_for_environments(projects) } render json: { projects: serialize_as_json_for_environments(projects) }
end end
end
end
# Used by Operations and Environments dashboard.
def create def create
respond_to do |format|
format.json do
project_ids = params['project_ids'] project_ids = params['project_ids']
result = add_projects(project_ids) result = add_projects(project_ids)
...@@ -40,7 +50,10 @@ class OperationsController < ApplicationController ...@@ -40,7 +50,10 @@ class OperationsController < ApplicationController
invalid: result.invalid_project_ids invalid: result.invalid_project_ids
} }
end end
end
end
# Used by Operations and Environments dashboard.
def destroy def destroy
project_id = params['project_id'] project_id = params['project_id']
...@@ -57,6 +70,10 @@ class OperationsController < ApplicationController ...@@ -57,6 +70,10 @@ class OperationsController < ApplicationController
render_404 unless can?(current_user, :read_operations_dashboard) render_404 unless can?(current_user, :read_operations_dashboard)
end end
def set_polling_interval_header
Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL)
end
def load_projects def load_projects
Dashboard::Operations::ListService.new(current_user).execute Dashboard::Operations::ListService.new(current_user).execute
end end
......
...@@ -6,8 +6,8 @@ module EE ...@@ -6,8 +6,8 @@ module EE
def operations_data def operations_data
{ {
'add-path' => add_operations_project_path, 'add-path' => add_operations_project_path(format: :json),
'list-path' => operations_list_path, 'list-path' => operations_path(format: :json),
'empty-dashboard-svg-path' => image_path('illustrations/operations-dashboard_empty.svg'), 'empty-dashboard-svg-path' => image_path('illustrations/operations-dashboard_empty.svg'),
'empty-dashboard-help-path' => help_page_path('user/operations_dashboard/index.md') 'empty-dashboard-help-path' => help_page_path('user/operations_dashboard/index.md')
} }
...@@ -15,8 +15,8 @@ module EE ...@@ -15,8 +15,8 @@ module EE
def environments_data def environments_data
{ {
'add-path' => add_operations_project_path, 'add-path' => add_operations_environments_project_path(format: :json),
'list-path' => operations_environments_list_path, 'list-path' => operations_environments_path(format: :json),
'empty-dashboard-svg-path' => image_path('illustrations/operations-dashboard_empty.svg'), 'empty-dashboard-svg-path' => image_path('illustrations/operations-dashboard_empty.svg'),
'empty-dashboard-help-path' => help_page_path('ci/environments/environments_dashboard.md'), 'empty-dashboard-help-path' => help_page_path('ci/environments/environments_dashboard.md'),
'environments-dashboard-help-path' => help_page_path('ci/environments/environments_dashboard.md') 'environments-dashboard-help-path' => help_page_path('ci/environments/environments_dashboard.md')
......
...@@ -9,7 +9,7 @@ class DashboardEnvironmentsProjectEntity < Grape::Entity ...@@ -9,7 +9,7 @@ class DashboardEnvironmentsProjectEntity < Grape::Entity
expose :web_url expose :web_url
expose :remove_path do |project| expose :remove_path do |project|
remove_operations_project_path(project_id: project.id) remove_operations_environments_project_path(project_id: project.id)
end end
expose :namespace, using: API::Entities::NamespaceBasic expose :namespace, using: API::Entities::NamespaceBasic
......
# frozen_string_literal: true # frozen_string_literal: true
# Used by Operations dashboard
get 'operations' => 'operations#index' get 'operations' => 'operations#index'
get 'operations/environments' => 'operations#environments'
get 'operations/list' => 'operations#list'
get 'operations/environments_list' => 'operations#environments_list'
post 'operations' => 'operations#create', as: :add_operations_project post 'operations' => 'operations#create', as: :add_operations_project
delete 'operations' => 'operations#destroy', as: :remove_operations_project delete 'operations' => 'operations#destroy', as: :remove_operations_project
# Used by Environments dashboard
get 'operations/environments' => 'operations#environments'
post 'operations/environments' => 'operations#create', as: :add_operations_environments_project
delete 'operations/environments' => 'operations#destroy', as: :remove_operations_environments_project
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Operations routing', 'routing' do
describe '/-/operations' do
it 'routes to the operations index action' do
expect(get("#{operations_path}.html")).to route_to(
controller: 'operations',
action: 'index',
format: 'html')
expect(get("#{operations_path}.json")).to route_to(
controller: 'operations',
action: 'index',
format: 'json')
end
it 'routes to the operations create action' do
expect(post("#{add_operations_project_path}.json")).to route_to(
controller: 'operations',
action: 'create',
format: 'json')
end
it 'routes to operations destroy action' do
expect(delete("#{remove_operations_project_path}.json")).to route_to(
controller: 'operations',
action: 'destroy',
format: 'json')
end
end
describe '/-/operations/environments' do
it 'routes to the environments list action' do
expect(get("#{operations_environments_path}.html")).to route_to(
controller: 'operations',
action: 'environments',
format: 'html')
expect(get("#{operations_environments_path}.json")).to route_to(
controller: 'operations',
action: 'environments',
format: 'json')
end
it 'routes to the environments create action' do
expect(post("#{add_operations_environments_project_path}.json")).to route_to(
controller: 'operations',
action: 'create',
format: 'json')
end
it 'routes to environments destroy action' do
expect(delete("#{remove_operations_environments_project_path}.json")).to route_to(
controller: 'operations',
action: 'destroy',
format: 'json')
end
end
end
...@@ -6,8 +6,8 @@ RSpec.describe 'operations/environments.html.haml' do ...@@ -6,8 +6,8 @@ RSpec.describe 'operations/environments.html.haml' do
it 'renders the frontend configuration' do it 'renders the frontend configuration' do
render render
expect(rendered).to match %r{data-add-path="/-/operations"} expect(rendered).to match %r{data-add-path="/-/operations/environments.json"}
expect(rendered).to match %r{data-list-path="/-/operations/environments_list"} expect(rendered).to match %r{data-list-path="/-/operations/environments.json"}
expect(rendered).to match %r{data-empty-dashboard-svg-path="/assets/illustrations/operations-dashboard_empty.*\.svg"} expect(rendered).to match %r{data-empty-dashboard-svg-path="/assets/illustrations/operations-dashboard_empty.*\.svg"}
expect(rendered).to match %r{data-empty-dashboard-help-path="/help/ci/environments/environments_dashboard.md"} expect(rendered).to match %r{data-empty-dashboard-help-path="/help/ci/environments/environments_dashboard.md"}
expect(rendered).to match %r{data-environments-dashboard-help-path="/help/ci/environments/environments_dashboard.md"} expect(rendered).to match %r{data-environments-dashboard-help-path="/help/ci/environments/environments_dashboard.md"}
......
...@@ -6,8 +6,8 @@ RSpec.describe 'operations/index.html.haml' do ...@@ -6,8 +6,8 @@ RSpec.describe 'operations/index.html.haml' do
it 'renders the frontend configuration' do it 'renders the frontend configuration' do
render render
expect(rendered).to match %r{data-add-path="/-/operations"} expect(rendered).to match %r{data-add-path="/-/operations.json"}
expect(rendered).to match %r{data-list-path="/-/operations/list"} expect(rendered).to match %r{data-list-path="/-/operations.json"}
expect(rendered).to match %{data-empty-dashboard-svg-path="/assets/illustrations/operations-dashboard_empty.*\.svg"} expect(rendered).to match %{data-empty-dashboard-svg-path="/assets/illustrations/operations-dashboard_empty.*\.svg"}
expect(rendered).to match %r{data-empty-dashboard-help-path="/help/user/operations_dashboard/index.md"} expect(rendered).to match %r{data-empty-dashboard-help-path="/help/user/operations_dashboard/index.md"}
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment