Commit 6d7078e7 authored by Bala Kumar's avatar Bala Kumar Committed by Shinya Maeda

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

parent b8376928
# 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
before_action :authorize_read_operations_dashboard!
respond_to :json, only: [:list]
feature_category :release_orchestration
POLLING_INTERVAL = 120_000
# Used by Operations dashboard.
def index
end
respond_to do |format|
format.html
def environments
end
format.json do
set_polling_interval_header
projects = load_projects
def list
Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL)
projects = load_projects
render json: { projects: serialize_as_json(projects) }
render json: { projects: serialize_as_json(projects) }
end
end
end
def environments_list
Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL)
projects = load_environments_projects
# Used by Environments dashboard.
def environments
respond_to do |format|
format.html
render json: { projects: serialize_as_json_for_environments(projects) }
format.json do
set_polling_interval_header
projects = load_environments_projects
render json: { projects: serialize_as_json_for_environments(projects) }
end
end
end
# Used by Operations and Environments dashboard.
def create
project_ids = params['project_ids']
result = add_projects(project_ids)
render json: {
added: result.added_project_ids,
duplicate: result.duplicate_project_ids,
invalid: result.invalid_project_ids
}
respond_to do |format|
format.json do
project_ids = params['project_ids']
result = add_projects(project_ids)
render json: {
added: result.added_project_ids,
duplicate: result.duplicate_project_ids,
invalid: result.invalid_project_ids
}
end
end
end
# Used by Operations and Environments dashboard.
def destroy
project_id = params['project_id']
......@@ -57,6 +70,10 @@ class OperationsController < ApplicationController
render_404 unless can?(current_user, :read_operations_dashboard)
end
def set_polling_interval_header
Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL)
end
def load_projects
Dashboard::Operations::ListService.new(current_user).execute
end
......
......@@ -6,8 +6,8 @@ module EE
def operations_data
{
'add-path' => add_operations_project_path,
'list-path' => operations_list_path,
'add-path' => add_operations_project_path(format: :json),
'list-path' => operations_path(format: :json),
'empty-dashboard-svg-path' => image_path('illustrations/operations-dashboard_empty.svg'),
'empty-dashboard-help-path' => help_page_path('user/operations_dashboard/index.md')
}
......@@ -15,8 +15,8 @@ module EE
def environments_data
{
'add-path' => add_operations_project_path,
'list-path' => operations_environments_list_path,
'add-path' => add_operations_environments_project_path(format: :json),
'list-path' => operations_environments_path(format: :json),
'empty-dashboard-svg-path' => image_path('illustrations/operations-dashboard_empty.svg'),
'empty-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
expose :web_url
expose :remove_path do |project|
remove_operations_project_path(project_id: project.id)
remove_operations_environments_project_path(project_id: project.id)
end
expose :namespace, using: API::Entities::NamespaceBasic
......
# frozen_string_literal: true
# Used by Operations dashboard
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
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
it 'renders the frontend configuration' do
render
expect(rendered).to match %r{data-add-path="/-/operations"}
expect(rendered).to match %r{data-list-path="/-/operations/environments_list"}
expect(rendered).to match %r{data-add-path="/-/operations/environments.json"}
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-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
it 'renders the frontend configuration' do
render
expect(rendered).to match %r{data-add-path="/-/operations"}
expect(rendered).to match %r{data-list-path="/-/operations/list"}
expect(rendered).to match %r{data-add-path="/-/operations.json"}
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 %r{data-empty-dashboard-help-path="/help/user/operations_dashboard/index.md"}
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