summaryrefslogtreecommitdiff
path: root/app/graphql/resolvers/alert_management/integrations_resolver.rb
blob: cb7e73c2d1a1936794d362fdc4f2efc8eadb8c99 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true

module Resolvers
  module AlertManagement
    class IntegrationsResolver < BaseResolver
      include ::Gitlab::Graphql::Laziness

      alias_method :project, :object

      argument :id, ::Types::GlobalIDType,
               required: false,
               description: 'ID of the integration.'

      type Types::AlertManagement::IntegrationType.connection_type, null: true

      def resolve(id: nil)
        if id
          integrations_by(gid: id)
        else
          http_integrations + prometheus_integrations
        end
      end

      private

      def integrations_by(gid:)
        object = GitlabSchema.object_from_id(gid, expected_type: expected_integration_types)
        defer { object }.then do |integration|
          ret = integration if project == integration&.project
          Array.wrap(ret)
        end
      end

      def prometheus_integrations
        return [] unless prometheus_integrations_allowed?

        Array(project.prometheus_service)
      end

      def http_integrations
        return [] unless http_integrations_allowed?

        ::AlertManagement::HttpIntegrationsFinder.new(project, {}).execute
      end

      def prometheus_integrations_allowed?
        Ability.allowed?(current_user, :admin_project, project)
      end

      def http_integrations_allowed?
        Ability.allowed?(current_user, :admin_operations, project)
      end

      def expected_integration_types
        [].tap do |types|
          types << ::AlertManagement::HttpIntegration if http_integrations_allowed?
          types << ::PrometheusService if prometheus_integrations_allowed?
        end
      end
    end
  end
end