Read about our upcoming Code of Conduct on this issue

pipelines.rb 4.86 KB
Newer Older
1
2
# frozen_string_literal: true

Z.J. van de Weg's avatar
Z.J. van de Weg committed
3
4
module API
  class Pipelines < Grape::API
5
6
    include PaginationParams

Z.J. van de Weg's avatar
Z.J. van de Weg committed
7
8
9
10
11
    before { authenticate! }

    params do
      requires :id, type: String, desc: 'The project ID'
    end
12
    resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
Z.J. van de Weg's avatar
Z.J. van de Weg committed
13
14
      desc 'Get all Pipelines of the project' do
        detail 'This feature was introduced in GitLab 8.11.'
15
        success Entities::PipelineBasic
Z.J. van de Weg's avatar
Z.J. van de Weg committed
16
17
      end
      params do
18
        use :pagination
Shinya Maeda's avatar
Shinya Maeda committed
19
        optional :scope,    type: String, values: %w[running pending finished branches tags],
Shinya Maeda's avatar
Shinya Maeda committed
20
                            desc: 'The scope of pipelines'
21
        optional :status,   type: String, values: HasStatus::AVAILABLE_STATUSES,
Shinya Maeda's avatar
Shinya Maeda committed
22
23
                            desc: 'The status of pipelines'
        optional :ref,      type: String, desc: 'The ref of pipelines'
24
        optional :sha,      type: String, desc: 'The sha of pipelines'
Shinya Maeda's avatar
Shinya Maeda committed
25
26
27
        optional :yaml_errors, type: Boolean, desc: 'Returns pipelines with invalid configurations'
        optional :name,     type: String, desc: 'The name of the user who triggered pipelines'
        optional :username, type: String, desc: 'The username of the user who triggered pipelines'
28
        optional :order_by, type: String, values: PipelinesFinder::ALLOWED_INDEXED_COLUMNS, default: 'id',
Shinya Maeda's avatar
Shinya Maeda committed
29
                            desc: 'Order pipelines'
30
        optional :sort,     type: String, values: %w[asc desc], default: 'desc',
Shinya Maeda's avatar
Shinya Maeda committed
31
                            desc: 'Sort pipelines'
Z.J. van de Weg's avatar
Z.J. van de Weg committed
32
33
34
35
      end
      get ':id/pipelines' do
        authorize! :read_pipeline, user_project

36
        pipelines = PipelinesFinder.new(user_project, current_user, params).execute
37
        present paginate(pipelines), with: Entities::PipelineBasic
Z.J. van de Weg's avatar
Z.J. van de Weg committed
38
      end
dimitrieh's avatar
dimitrieh committed
39

40
41
42
43
44
45
      desc 'Create a new pipeline' do
        detail 'This feature was introduced in GitLab 8.14'
        success Entities::Pipeline
      end
      params do
        requires :ref, type: String,  desc: 'Reference'
46
        optional :variables, Array, desc: 'Array of variables available in the pipeline'
47
      end
48
      # rubocop: disable CodeReuse/ActiveRecord
49
      post ':id/pipeline' do
50
51
        Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42124')

52
53
        authorize! :create_pipeline, user_project

Jacopo's avatar
Jacopo committed
54
55
56
57
        pipeline_params = declared_params(include_missing: false)
          .merge(variables_attributes: params[:variables])
          .except(:variables)

58
59
        new_pipeline = Ci::CreatePipelineService.new(user_project,
                                                     current_user,
Jacopo's avatar
Jacopo committed
60
                                                     pipeline_params)
61
                           .execute(:api, ignore_skip_ci: true, save_on_errors: false)
62

63
64
65
66
67
68
        if new_pipeline.persisted?
          present new_pipeline, with: Entities::Pipeline
        else
          render_validation_error!(new_pipeline)
        end
      end
69
      # rubocop: enable CodeReuse/ActiveRecord
Z.J. van de Weg's avatar
Z.J. van de Weg committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83

      desc 'Gets a specific pipeline for the project' do
        detail 'This feature was introduced in GitLab 8.11'
        success Entities::Pipeline
      end
      params do
        requires :pipeline_id, type: Integer, desc: 'The pipeline ID'
      end
      get ':id/pipelines/:pipeline_id' do
        authorize! :read_pipeline, user_project

        present pipeline, with: Entities::Pipeline
      end

84
85
86
87
88
89
90
91
      desc 'Deletes a pipeline' do
        detail 'This feature was introduced in GitLab 11.6'
        http_codes [[204, 'Pipeline was deleted'], [403, 'Forbidden']]
      end
      params do
        requires :pipeline_id, type: Integer, desc: 'The pipeline ID'
      end
      delete ':id/pipelines/:pipeline_id' do
92
        authorize! :destroy_pipeline, pipeline
93

Matija Čupić's avatar
Matija Čupić committed
94
95
96
        destroy_conditionally!(pipeline) do
          ::Ci::DestroyPipelineService.new(user_project, current_user).execute(pipeline)
        end
97
98
      end

dimitrieh's avatar
dimitrieh committed
99
      desc 'Retry builds in the pipeline' do
Z.J. van de Weg's avatar
Z.J. van de Weg committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        detail 'This feature was introduced in GitLab 8.11.'
        success Entities::Pipeline
      end
      params do
        requires :pipeline_id, type: Integer,  desc: 'The pipeline ID'
      end
      post ':id/pipelines/:pipeline_id/retry' do
        authorize! :update_pipeline, user_project

        pipeline.retry_failed(current_user)

        present pipeline, with: Entities::Pipeline
      end

      desc 'Cancel all builds in the pipeline' do
        detail 'This feature was introduced in GitLab 8.11.'
        success Entities::Pipeline
      end
      params do
        requires :pipeline_id, type: Integer,  desc: 'The pipeline ID'
      end
      post ':id/pipelines/:pipeline_id/cancel' do
        authorize! :update_pipeline, user_project

        pipeline.cancel_running

        status 200
        present pipeline.reload, with: Entities::Pipeline
      end
    end

    helpers do
      def pipeline
133
        @pipeline ||= user_project.ci_pipelines.find(params[:pipeline_id])
Z.J. van de Weg's avatar
Z.J. van de Weg committed
134
135
136
137
      end
    end
  end
end