Welcome to mirror list, hosted at ThFree Co, Russian Federation.

update_service.rb « http_integrations « alert_management « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 220c4e759f090072c5fcba073683f193462ff50e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

module AlertManagement
  module HttpIntegrations
    class UpdateService
      # @param integration [AlertManagement::HttpIntegration]
      # @param current_user [User]
      # @param params [Hash]
      def initialize(integration, current_user, params)
        @integration = integration
        @current_user = current_user
        @params = params
      end

      def execute
        return error_no_permissions unless allowed?

        params[:token] = nil if params.delete(:regenerate_token)

        if integration.update(params)
          success
        else
          error(integration.errors.full_messages.to_sentence)
        end
      end

      private

      attr_reader :integration, :current_user, :params

      def allowed?
        current_user&.can?(:admin_operations, integration)
      end

      def error(message)
        ServiceResponse.error(message: message)
      end

      def success
        ServiceResponse.success(payload: { integration: integration.reset })
      end

      def error_no_permissions
        error(_('You have insufficient permissions to update this HTTP integration'))
      end
    end
  end
end