Commit 0383b6fe authored by Nathan Friend's avatar Nathan Friend

Merge branch '209825-crud-for-group-level-integration-settings' into 'master'

Add CRUD for Group Level Integration Settings

See merge request gitlab-org/gitlab!27557
parents 8f2247be ec3f7f54
import IntegrationSettingsForm from '~/integrations/integration_settings_form';
import PrometheusMetrics from '~/prometheus_metrics/prometheus_metrics';
import initAlertsSettings from '~/alerts_service_settings';
document.addEventListener('DOMContentLoaded', () => {
const prometheusSettingsWrapper = document.querySelector('.js-prometheus-metrics-monitoring');
const integrationSettingsForm = new IntegrationSettingsForm('.js-integration-settings-form');
integrationSettingsForm.init();
if (prometheusSettingsWrapper) {
const prometheusMetrics = new PrometheusMetrics('.js-prometheus-metrics-monitoring');
prometheusMetrics.loadActiveMetrics();
}
initAlertsSettings(document.querySelector('.js-alerts-service-settings'));
});
# frozen_string_literal: true
class Admin::IntegrationsController < Admin::ApplicationController
include ServiceParams
before_action :not_found, unless: :instance_level_integrations_enabled?
before_action :service, only: [:edit, :update, :test]
def edit
end
def update
@service.attributes = service_params[:service]
if @service.save(context: :manual_change)
redirect_to edit_admin_application_settings_integration_path(@service), notice: success_message
else
render :edit
end
end
def test
if @service.can_test?
render json: service_test_response, status: :ok
else
render json: {}, status: :not_found
end
end
include IntegrationsActions
private
def instance_level_integrations_enabled?
def integrations_enabled?
Feature.enabled?(:instance_level_integrations)
end
def project
# TODO: Change to something more meaningful
Project.first
end
def service
@service ||= project.find_or_initialize_service(params[:id])
end
def success_message
message = @service.active? ? _('activated') : _('settings saved, but not activated')
_('%{service_title} %{message}.') % { service_title: @service.title, message: message }
end
def service_test_response
unless @service.update(service_params[:service])
return { error: true, message: _('Validations failed.'), service_response: @service.errors.full_messages.join(','), test_failed: false }
end
data = @service.test_data(project, current_user)
outcome = @service.test(data)
unless outcome[:success]
return { error: true, message: _('Test failed.'), service_response: outcome[:result].to_s, test_failed: true }
end
{}
rescue Gitlab::HTTP::BlockedUrlError => e
{ error: true, message: _('Test failed.'), service_response: e.message, test_failed: true }
def scoped_edit_integration_path(integration)
edit_admin_application_settings_integration_path(integration)
end
end
# frozen_string_literal: true
module IntegrationsActions
extend ActiveSupport::Concern
included do
include ServiceParams
before_action :not_found, unless: :integrations_enabled?
before_action :integration, only: [:edit, :update, :test]
end
def edit
render 'shared/integrations/edit'
end
def update
integration.attributes = service_params[:service]
saved = integration.save(context: :manual_change)
respond_to do |format|
format.html do
if saved
redirect_to scoped_edit_integration_path(integration), notice: success_message
else
render 'shared/integrations/edit'
end
end
format.json do
status = saved ? :ok : :unprocessable_entity
render json: serialize_as_json, status: status
end
end
end
def test
if integration.can_test?
render json: service_test_response, status: :ok
else
render json: {}, status: :not_found
end
end
private
def integrations_enabled?
false
end
# TODO: Use actual integrations on the group / instance level
# To be completed in https://gitlab.com/groups/gitlab-org/-/epics/2430
def project
Project.first
end
def integration
# Using instance variable `@service` still required as it's used in ServiceParams
# and app/views/shared/_service_settings.html.haml. Should be removed once
# those 2 are refactored to use `@integration`.
@integration = @service ||= project.find_or_initialize_service(params[:id]) # rubocop:disable Gitlab/ModuleWithInstanceVariables
end
def success_message
message = integration.active? ? _('activated') : _('settings saved, but not activated')
_('%{service_title} %{message}.') % { service_title: integration.title, message: message }
end
def serialize_as_json
integration
.as_json(only: integration.json_fields)
.merge(errors: integration.errors.as_json)
end
def service_test_response
unless integration.update(service_params[:service])
return { error: true, message: _('Validations failed.'), service_response: integration.errors.full_messages.join(','), test_failed: false }
end
data = integration.test_data(project, current_user)
outcome = integration.test(data)
unless outcome[:success]
return { error: true, message: _('Test failed.'), service_response: outcome[:result].to_s, test_failed: true }
end
{}
rescue Gitlab::HTTP::BlockedUrlError => e
{ error: true, message: _('Test failed.'), service_response: e.message, test_failed: true }
end
end
# frozen_string_literal: true
module Groups
module Settings
class IntegrationsController < Groups::ApplicationController
include IntegrationsActions
before_action :authorize_admin_group!
private
def integrations_enabled?
Feature.enabled?(:group_level_integrations, group)
end
def scoped_edit_integration_path(integration)
edit_group_settings_integration_path(group, integration)
end
end
end
end
......@@ -66,6 +66,36 @@ module ServicesHelper
edit_admin_application_settings_integration_path(integration)
end
def scoped_integrations_path
if @project.present?
project_settings_integrations_path(@project)
elsif @group.present?
group_settings_integrations_path(@group)
else
integrations_admin_application_settings_path
end
end
def scoped_integration_path(integration)
if @project.present?
project_settings_integration_path(@project, integration)
elsif @group.present?
group_settings_integration_path(@group, integration)
else
admin_application_settings_integration_path(integration)
end
end
def scoped_test_integration_path(integration)
if @project.present?
test_project_settings_integration_path(@project, integration)
elsif @group.present?
test_group_settings_integration_path(@group, integration)
else
test_admin_application_settings_integration_path(integration)
end
end
extend self
end
......
%h3.page-title
= @service.title
%p= @service.description
= form_for @service, as: :service, url: admin_application_settings_integration_path, method: :put, html: { class: 'gl-show-field-errors fieldset-form integration-settings-form js-integration-settings-form', data: { 'can-test' => @service.can_test?, 'test-url' => test_admin_application_settings_integration_path(@service) } } do |form|
= render 'shared/service_settings', form: form, service: @service
- if @service.editable?
.footer-block.row-content-block
= service_save_button(@service)
= link_to _('Cancel'), admin_application_settings_integration_path, class: 'btn btn-cancel'
- add_to_breadcrumbs _('Integrations'), integrations_admin_application_settings_path
- breadcrumb_title @service.title
- page_title @service.title, _('Integrations')
= render 'form'
- integration = local_assigns.fetch(:integration)
%h3.page-title
= integration.title
%p= integration.description
= form_for integration, as: :service, url: scoped_integration_path(integration), method: :put, html: { class: 'gl-show-field-errors fieldset-form integration-settings-form js-integration-settings-form', data: { 'can-test' => integration.can_test?, 'test-url' => scoped_test_integration_path(integration) } } do |form|
= render 'shared/service_settings', form: form, integration: integration
- if integration.editable?
.footer-block.row-content-block
= service_save_button(integration)
= link_to _('Cancel'), scoped_integration_path(integration), class: 'btn btn-cancel'
- add_to_breadcrumbs _('Integrations'), scoped_integrations_path
- breadcrumb_title @integration.title
- page_title @integration.title, _('Integrations')
= render 'shared/integrations/form', integration: @integration
......@@ -121,7 +121,7 @@ namespace :admin do
get '/', to: redirect('admin/application_settings/general'), as: nil
resources :services, only: [:index, :edit, :update]
resources :integrations, only: [:edit, :update, :test] do
resources :integrations, only: [:edit, :update] do
member do
put :test
end
......
......@@ -31,6 +31,12 @@ constraints(::Constraints::GroupUrlConstrainer.new) do
patch :update_auto_devops
post :create_deploy_token, path: 'deploy_token/create'
end
resources :integrations, only: [:index, :edit, :update] do
member do
put :test
end
end
end
resource :variables, only: [:show, :update]
......
......@@ -3,8 +3,8 @@
require 'spec_helper'
describe Admin::IntegrationsController do
let_it_be(:project) { create(:project) }
let(:admin) { create(:admin) }
let!(:project) { create(:project) }
before do
sign_in(admin)
......@@ -13,7 +13,7 @@ describe Admin::IntegrationsController do
describe '#edit' do
context 'when instance_level_integrations not enabled' do
it 'returns not_found' do
allow(Feature).to receive(:enabled?).with(:instance_level_integrations) { false }
stub_feature_flags(instance_level_integrations: false)
get :edit, params: { id: Service.available_services_names.sample }
......
# frozen_string_literal: true
require 'spec_helper'
describe Groups::Settings::IntegrationsController do
let_it_be(:project) { create(:project) }
let(:user) { create(:user) }
let(:group) { create(:group) }
before do
sign_in(user)
end
describe '#edit' do
context 'when group_level_integrations not enabled' do
it 'returns not_found' do
stub_feature_flags(group_level_integrations: { enabled: false, thing: group })
get :edit, params: { group_id: group, id: Service.available_services_names.sample }
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when user is not owner' do
it 'renders not_found' do
get :edit, params: { group_id: group, id: Service.available_services_names.sample }
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when user is owner' do
before do
group.add_owner(user)
end
Service.available_services_names.each do |integration_name|
context "#{integration_name}" do
it 'successfully displays the template' do
get :edit, params: { group_id: group, id: integration_name }
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:edit)
end
end
end
end
end
describe '#update' do
let(:integration) { create(:jira_service, project: project) }
before do
group.add_owner(user)
put :update, params: { group_id: group, id: integration.class.to_param, service: { url: url } }
end
context 'valid params' do
let(:url) { 'https://jira.gitlab-example.com' }
it 'updates the integration' do
expect(response).to have_gitlab_http_status(:found)
expect(integration.reload.url).to eq(url)
end
end
context 'invalid params' do
let(:url) { 'ftp://jira.localhost' }
it 'does not update the integration' do
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:edit)
expect(integration.reload.url).not_to eq(url)
end
end
end
describe '#test' do
context 'testable' do
let(:integration) { create(:jira_service, project: project) }
before do
group.add_owner(user)
end
it 'returns ok' do
allow_any_instance_of(integration.class).to receive(:test) { { success: true } }
put :test, params: { group_id: group, id: integration.class.to_param }
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'not testable' do
let(:integration) { create(:alerts_service, project: project) }
it 'returns not found' do
put :test, params: { group_id: group, id: integration.class.to_param }
expect(response).to have_gitlab_http_status(:not_found)
end
end
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