summaryrefslogtreecommitdiff
path: root/lib/api/projects.rb
blob: c60d619a34fcf7251db5df4fe1e13c5458a52db7 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
module API
  # Projects API
  class Projects < Grape::API
    before { authenticate! }

    resource :projects do
      # Register new webhook for project
      #
      # Parameters
      #   project_id (required) - The ID of a project
      #   web_hook (required) - WebHook URL
      # Example Request
      #   POST /projects/:project_id/webhooks
      post ":project_id/webhooks" do
        required_attributes! [:web_hook]

        project = Project.find(params[:project_id])

        if project.present? && current_user.can_access_project?(project.gitlab_id)
          web_hook = project.web_hooks.new({ url: params[:web_hook] })

          if web_hook.save
            present web_hook, with: Entities::WebHook
          else
            errors = web_hook.errors.full_messages.join(", ")
            render_api_error!(errors, 400)
          end
        end
      end

      # Retrieve all jobs for a project
      #
      # Parameters
      #   id (required) - The ID of a project
      # Example Request
      #   GET /projects/:id/jobs
      get ":id/jobs" do
        project = Project.find(params[:id])

        not_found! if project.blank?
        unauthorized! unless current_user.can_access_project?(project.gitlab_id)

        project.jobs
      end

      # Add a new job to a project
      #
      # Parameters
      #   id (required) - The ID of a project
      #   name (required) - The job name
      #   commands (required) - The command line script for the job
      #   active (optional) - The command is active of not
      #   build_branches (optional) - Trigger commit builds
      #   build_tags (optional) - Trigger tag builds
      #   tags (optional) - The tags associated with this job
      # Example Request
      #   POST /projects/:id/jobs
      post ":id/jobs" do
        required_attributes! [:name, :commands]

        project = Project.find(params[:id])

        not_found! if project.blank?
        unauthorized! unless current_user.can_access_project?(project.gitlab_id)

        job_params =
        {
          name: params[:name],
          commands: params[:commands],
        }

        job_params[:active] = params[:active] unless params[:active].nil?
        job_params[:build_branches] = params[:build_branches] unless params[:build_branches].nil?
        job_params[:build_tags] = params[:build_tags] unless params[:build_tags].nil?
        job_params[:tag_list] = params[:tags] unless params[:tags].nil?

        job = project.jobs.new(job_params)
        if job.save
          present job, with: Entities::Job
        else
          errors = job.errors.full_messages.join(", ")
          render_api_error!(errors, 400)
        end
      end

      # Delete a job for a project
      #
      # Parameters
      #   id (required) - The ID of a project
      #   job_id (required) - The ID of the job to delete
      # Example Request
      #   DELETE /projects/:id/jobs/:job_id
      delete ":id/jobs/:job_id" do
        required_attributes! [:job_id]

        project = Project.find(params[:id])
        job     = project.jobs.find(params[:job_id])

        not_found! if project.blank? || job.blank?
        unauthorized! unless current_user.can_access_project?(project.gitlab_id)

        job.destroy
      end

      # Retrieve all Gitlab CI projects that the user has access to
      #
      # Example Request:
      #   GET /projects
      get do
        gitlab_projects = Project.from_gitlab(
          current_user, params[:page], params[:per_page], :authorized
        )
        ids = gitlab_projects.map { |project| project.id }

        projects = Project.where("gitlab_id IN (?)", ids).load
        present projects, with: Entities::Project
      end

      # Retrieve all Gitlab CI projects that the user owns
      #
      # Example Request:
      #   GET /projects/owned
      get "owned" do
        gitlab_projects = Project.from_gitlab(
          current_user, params[:page], params[:per_page], :owned
        )
        ids = gitlab_projects.map { |project| project.id }

        projects = Project.where("gitlab_id IN (?)", ids).load
        present projects, with: Entities::Project
      end

      # Retrieve info for a Gitlab CI project
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
      #   GET /projects/:id
      get ":id" do
        project = Project.find(params[:id])

        if current_user.can_access_project?(project.gitlab_id)
          present project, with: Entities::Project
        else
          unauthorized!
        end
      end

      # Create Gitlab CI project using Gitlab project info
      #
      # Parameters:
      #   name (required)            - The name of the project
      #   gitlab_id (required)       - The gitlab id of the project
      #   gitlab_url (required)      - The gitlab web url to the project
      #   ssh_url_to_repo (required) - The gitlab ssh url to the repo
      #   default_ref                - The branch to run against (defaults to `master`)
      # Example Request:
      #   POST /projects
      post do
        required_attributes! [:name, :gitlab_id, :gitlab_url, :ssh_url_to_repo]

        filtered_params = {
          name:            params[:name],
          gitlab_id:       params[:gitlab_id],
          gitlab_url:      params[:gitlab_url],
          default_ref:     params[:default_ref] || 'master',
          ssh_url_to_repo: params[:ssh_url_to_repo]
        }

        project = Project.new(filtered_params)
        project.build_missing_services
        project.build_default_job

        if project.save
          present project, with: Entities::Project
        else
          errors = project.errors.full_messages.join(", ")
          render_api_error!(errors, 400)
        end
      end

      # Update a Gitlab CI project
      #
      # Parameters:
      #   id (required)   - The ID of a project
      #   name            - The name of the project
      #   gitlab_id       - The gitlab id of the project
      #   gitlab_url      - The gitlab web url to the project
      #   ssh_url_to_repo - The gitlab ssh url to the repo
      #   default_ref     - The branch to run against (defaults to `master`)
      # Example Request:
      #   PUT /projects/:id
      put ":id" do
        project = Project.find(params[:id])

        if project.present? && current_user.can_access_project?(project.gitlab_id)
          attrs = attributes_for_keys [:name, :gitlab_id, :gitlab_url, :default_ref, :ssh_url_to_repo]

          if project.update_attributes(attrs)
            present project, with: Entities::Project
          else
            errors = project.errors.full_messages.join(", ")
            render_api_error!(errors, 400)
          end
        else
          not_found!
        end
      end

      # Remove a Gitlab CI project
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
      #   DELETE /projects/:id
      delete ":id" do
        project = Project.find(params[:id])

        if project.present? && current_user.can_access_project?(project.gitlab_id)
          project.destroy
        else
          not_found!
        end
      end

      # Link a Gitlab CI project to a runner
      #
      # Parameters:
      #   id (required) - The ID of a CI project
      #   runner_id (required) - The ID of a runner
      # Example Request:
      #   POST /projects/:id/runners/:runner_id
      post ":id/runners/:runner_id" do
        project = Project.find_by_id(params[:id])
        runner  = Runner.find_by_id(params[:runner_id])

        not_found! if project.blank? or runner.blank?

        unauthorized! unless current_user.can_access_project?(project.gitlab_id)

        options = {
          project_id: project.id,
          runner_id:  runner.id
        }

        runner_project = RunnerProject.new(options)

        if runner_project.save
          present runner_project, with: Entities::RunnerProject
        else
          errors = project.errors.full_messages.join(", ")
          render_api_error!(errors, 400)
        end
      end

      # Remove a Gitlab CI project from a runner
      #
      # Parameters:
      #   id (required) - The ID of a CI project
      #   runner_id (required) - The ID of a runner
      # Example Request:
      #   DELETE /projects/:id/runners/:runner_id
      delete ":id/runners/:runner_id" do
        project = Project.find_by_id(params[:id])
        runner  = Runner.find_by_id(params[:runner_id])

        not_found! if project.blank? or runner.blank?
        unauthorized! unless current_user.can_access_project?(project.gitlab_id)

        options = {
          project_id: project.id,
          runner_id:  runner.id
        }

        runner_project = RunnerProject.where(options).first

        if runner_project.present?
          runner_project.destroy
        else
          not_found!
        end
      end
    end
  end
end