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

create_service.rb « annotations « dashboard « metrics « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 47e9afa36b9f75036bb6956594393e1f40a7166f (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

# Create Metrics::Dashboard::Annotation entry based on matched dashboard_path, environment, cluster
module Metrics
  module Dashboard
    module Annotations
      class CreateService < ::BaseService
        include Stepable

        steps :authorize_environment_access,
              :authorize_cluster_access,
              :parse_dashboard_path,
              :create

        def initialize(user, params)
          @user = user
          @params = params
        end

        def execute
          execute_steps
        end

        private

        attr_reader :user, :params

        def authorize_environment_access(options)
          if environment.nil? || Ability.allowed?(user, :admin_metrics_dashboard_annotation, project)
            options[:environment] = environment
            success(options)
          else
            error(s_('MetricsDashboardAnnotation|You are not authorized to create annotation for selected environment'))
          end
        end

        def authorize_cluster_access(options)
          if cluster.nil? || Ability.allowed?(user, :admin_metrics_dashboard_annotation, cluster)
            options[:cluster] = cluster
            success(options)
          else
            error(s_('MetricsDashboardAnnotation|You are not authorized to create annotation for selected cluster'))
          end
        end

        def parse_dashboard_path(options)
          dashboard_path = params[:dashboard_path]

          Gitlab::Metrics::Dashboard::Finder.find_raw(project, dashboard_path: dashboard_path)
          options[:dashboard_path] = dashboard_path

          success(options)
        rescue Gitlab::Template::Finders::RepoTemplateFinder::FileNotFoundError
          error(s_('MetricsDashboardAnnotation|Dashboard with requested path can not be found'))
        end

        def create(options)
          annotation = Annotation.new(options.slice(:environment, :cluster, :dashboard_path).merge(params.slice(:description, :starting_at, :ending_at)))

          if annotation.save
            success(annotation: annotation)
          else
            error(annotation.errors)
          end
        end

        def environment
          params[:environment]
        end

        def cluster
          params[:cluster]
        end

        def project
          (environment || cluster)&.project
        end
      end
    end
  end
end