BigW Consortium Gitlab

pipeline_schedules.rb 4.72 KB
Newer Older
1 2 3 4
module API
  class PipelineSchedules < Grape::API
    include PaginationParams

5 6
    before { authenticate! }

7 8 9 10
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
    resource :projects, requirements: { id: %r{[^/]+} } do
Shinya Maeda committed
11
      desc 'Get all pipeline schedules' do
12 13 14 15
        success Entities::PipelineSchedule
      end
      params do
        use :pagination
16 17
        optional :scope,    type: String, values: %w[active inactive],
                            desc: 'The scope of pipeline schedules'
18 19 20 21
      end
      get ':id/pipeline_schedules' do
        authorize! :read_pipeline_schedule, user_project

22 23 24
        schedules = PipelineSchedulesFinder.new(user_project).execute(scope: params[:scope])
          .preload([:owner, :last_pipeline])
        present paginate(schedules), with: Entities::PipelineSchedule
25 26
      end

Shinya Maeda committed
27
      desc 'Get a single pipeline schedule' do
28
        success Entities::PipelineScheduleDetails
29 30
      end
      params do
Shinya Maeda committed
31
        requires :pipeline_schedule_id, type: Integer,  desc: 'The pipeline schedule id'
32 33 34 35
      end
      get ':id/pipeline_schedules/:pipeline_schedule_id' do
        authorize! :read_pipeline_schedule, user_project

36
        not_found!('PipelineSchedule') unless pipeline_schedule
37

38
        present pipeline_schedule, with: Entities::PipelineScheduleDetails
39 40
      end

Shinya Maeda committed
41
      desc 'Create a new pipeline schedule' do
42
        success Entities::PipelineScheduleDetails
43 44
      end
      params do
Shinya Maeda committed
45 46 47
        requires :description, type: String, desc: 'The description of pipeline schedule'
        requires :ref, type: String, desc: 'The branch/tag name will be triggered'
        requires :cron, type: String, desc: 'The cron'
48 49
        optional :cron_timezone, type: String, default: 'UTC', desc: 'The timezone'
        optional :active, type: Boolean, default: true, desc: 'The activation of pipeline schedule'
50 51 52 53
      end
      post ':id/pipeline_schedules' do
        authorize! :create_pipeline_schedule, user_project

54 55 56
        pipeline_schedule = Ci::CreatePipelineScheduleService
          .new(user_project, current_user, declared_params(include_missing: false))
          .execute
57

Shinya Maeda committed
58
        if pipeline_schedule.persisted?
59
          present pipeline_schedule, with: Entities::PipelineScheduleDetails
60 61 62 63 64
        else
          render_validation_error!(pipeline_schedule)
        end
      end

Shinya Maeda committed
65
      desc 'Edit a pipeline schedule' do
66
        success Entities::PipelineScheduleDetails
67 68
      end
      params do
Shinya Maeda committed
69 70 71 72 73 74
        requires :pipeline_schedule_id, type: Integer,  desc: 'The pipeline schedule id'
        optional :description, type: String, desc: 'The description of pipeline schedule'
        optional :ref, type: String, desc: 'The branch/tag name will be triggered'
        optional :cron, type: String, desc: 'The cron'
        optional :cron_timezone, type: String, desc: 'The timezone'
        optional :active, type: Boolean, desc: 'The activation of pipeline schedule'
75 76
      end
      put ':id/pipeline_schedules/:pipeline_schedule_id' do
77
        authorize! :update_pipeline_schedule, user_project
78

79
        not_found!('PipelineSchedule') unless pipeline_schedule
80 81

        if pipeline_schedule.update(declared_params(include_missing: false))
82
          present pipeline_schedule, with: Entities::PipelineScheduleDetails
83 84 85 86 87
        else
          render_validation_error!(pipeline_schedule)
        end
      end

Shinya Maeda committed
88
      desc 'Take ownership of a pipeline schedule' do
89
        success Entities::PipelineScheduleDetails
90 91
      end
      params do
Shinya Maeda committed
92
        requires :pipeline_schedule_id, type: Integer,  desc: 'The pipeline schedule id'
93 94
      end
      post ':id/pipeline_schedules/:pipeline_schedule_id/take_ownership' do
95
        authorize! :update_pipeline_schedule, user_project
96

97
        not_found!('PipelineSchedule') unless pipeline_schedule
98

99
        if pipeline_schedule.own!(current_user)
100
          present pipeline_schedule, with: Entities::PipelineScheduleDetails
101 102 103 104 105
        else
          render_validation_error!(pipeline_schedule)
        end
      end

Shinya Maeda committed
106
      desc 'Delete a pipeline schedule' do
107
        success Entities::PipelineScheduleDetails
108 109
      end
      params do
Shinya Maeda committed
110
        requires :pipeline_schedule_id, type: Integer,  desc: 'The pipeline schedule id'
111 112 113 114
      end
      delete ':id/pipeline_schedules/:pipeline_schedule_id' do
        authorize! :admin_pipeline_schedule, user_project

115
        not_found!('PipelineSchedule') unless pipeline_schedule
116

117
        status :accepted
118
        present pipeline_schedule.destroy, with: Entities::PipelineScheduleDetails
119 120
      end
    end
121 122 123 124 125

    helpers do
      def pipeline_schedule
        @pipeline_schedule ||=
          user_project.pipeline_schedules
126
                      .preload(:owner, :last_pipeline)
127
                      .find_by(id: params.delete(:pipeline_schedule_id))
128 129
      end
    end
130 131
  end
end