Commit 7b16cf16 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch '37791-merge-licenses-controllers' into 'master'

Merge licenses controllers

See merge request gitlab-org/gitlab!21087
parents 09dd832f 17f1854f
...@@ -2,10 +2,67 @@ ...@@ -2,10 +2,67 @@
module Projects module Projects
class LicensesController < Projects::ApplicationController class LicensesController < Projects::ApplicationController
before_action :authorize_read_licenses! before_action :authorize_read_licenses!, only: [:index]
before_action :authorize_admin_software_license_policy!, only: [:create, :update]
before_action do def index
push_frontend_feature_flag(:licenses_list) respond_to do |format|
format.html do
render status: :ok
end
format.json do
::Gitlab::UsageDataCounters::LicensesList.count(:views)
license_compliance = project.license_compliance
render json: serializer.represent(
pageable(license_compliance.policies),
build: license_compliance.latest_build_for_default_branch
)
end
end
end
def create
result = ::Projects::Licenses::CreatePolicyService
.new(project, current_user, software_license_policy_params)
.execute
if result[:status] == :success
render json: LicenseEntity.represent(result[:software_license_policy]), status: :created
else
render_error_for(result)
end
end
def update
result = ::Projects::Licenses::UpdatePolicyService
.new(project, current_user, software_license_policy_params)
.execute(params[:id])
if result[:status] == :success
render json: LicenseEntity.represent(result[:software_license_policy]), status: :ok
else
render_error_for(result)
end
end
private
def serializer
::LicensesListSerializer.new(project: project, user: current_user)
.with_pagination(request, response)
end
def pageable(items)
::Gitlab::ItemsCollection.new(items)
end
def software_license_policy_params
params.require(:software_license_policy).permit(:software_license_id, :spdx_identifier, :classification)
end
def render_error_for(result)
render json: { errors: result[:message].as_json }, status: result.fetch(:http_status, :unprocessable_entity)
end end
end end
end end
# frozen_string_literal: true
module Projects
module Security
class LicensesController < Projects::ApplicationController
before_action :authorize_read_licenses!, only: [:index]
before_action :authorize_admin_software_license_policy!, only: [:create, :update]
def index
respond_to do |format|
format.json do
::Gitlab::UsageDataCounters::LicensesList.count(:views)
license_compliance = project.license_compliance
render json: serializer.represent(
pageable(license_compliance.policies),
build: license_compliance.latest_build_for_default_branch
)
end
end
end
def create
result = ::Projects::Licenses::CreatePolicyService
.new(project, current_user, software_license_policy_params)
.execute
if result[:status] == :success
render json: LicenseEntity.represent(result[:software_license_policy]), status: :created
else
render_error_for(result)
end
end
def update
result = ::Projects::Licenses::UpdatePolicyService
.new(project, current_user, software_license_policy_params)
.execute(params[:id])
if result[:status] == :success
render json: LicenseEntity.represent(result[:software_license_policy]), status: :ok
else
render_error_for(result)
end
end
private
def serializer
::LicensesListSerializer.new(project: project, user: current_user)
.with_pagination(request, response)
end
def pageable(items)
::Gitlab::ItemsCollection.new(items)
end
def software_license_policy_params
params.require(:software_license_policy).permit(:software_license_id, :spdx_identifier, :classification)
end
def render_error_for(result)
render json: { errors: result[:message].as_json }, status: result.fetch(:http_status, :unprocessable_entity)
end
end
end
end
- breadcrumb_title _('License Compliance') - breadcrumb_title _('License Compliance')
- page_title _('License Compliance') - page_title _('License Compliance')
#js-licenses-app{ data: { endpoint: project_security_licenses_path(@project), #js-licenses-app{ data: { endpoint: project_licenses_path(@project, format: :json),
documentation_path: help_page_path('user/application_security/license_compliance/index'), documentation_path: help_page_path('user/application_security/license_compliance/index'),
empty_state_svg_path: image_path('illustrations/Dependency-list-empty-state.svg') } } empty_state_svg_path: image_path('illustrations/Dependency-list-empty-state.svg') } }
...@@ -65,10 +65,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do ...@@ -65,10 +65,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
resources :dashboards, only: [:create] resources :dashboards, only: [:create]
end end
resource :licenses, only: [:show]
namespace :security do
resources :licenses, only: [:index, :create, :update] resources :licenses, only: [:index, :create, :update]
end
resource :threat_monitoring, only: [:show], controller: :threat_monitoring resource :threat_monitoring, only: [:show], controller: :threat_monitoring
......
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