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

create_service.rb « pipeline_schedules « ci « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c1825865bc022fae2776502d11998af20655159c (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
# frozen_string_literal: true

module Ci
  module PipelineSchedules
    class CreateService
      def initialize(project, user, params)
        @project = project
        @user = user
        @params = params

        @schedule = project.pipeline_schedules.new
      end

      def execute
        return forbidden unless allowed?

        schedule.assign_attributes(params.merge(owner: user))

        if schedule.save
          ServiceResponse.success(payload: schedule)
        else
          ServiceResponse.error(payload: schedule, message: schedule.errors.full_messages)
        end
      end

      private

      attr_reader :project, :user, :params, :schedule

      def allowed?
        user.can?(:create_pipeline_schedule, schedule)
      end

      def forbidden
        # We add the error to the base object too
        # because model errors are used in the API responses and the `form_errors` helper.
        schedule.errors.add(:base, forbidden_message)

        ServiceResponse.error(payload: schedule, message: [forbidden_message], reason: :forbidden)
      end

      def forbidden_message
        _('The current user is not authorized to create the pipeline schedule')
      end
    end
  end
end