projects.rb 14.0 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
2 3 4 5 6
  # Projects API
  class Projects < Grape::API
    before { authenticate! }

    resource :projects do
7 8 9 10 11 12 13 14 15
      helpers do
        def handle_project_member_errors(errors)
          if errors[:project_access].any?
            error!(errors[:project_access], 422)
          end
          not_found!
        end
      end

N
Nihad Abbasov 已提交
16 17 18 19 20
      # Get a projects list for authenticated user
      #
      # Example Request:
      #   GET /projects
      get do
21
        @projects = paginate current_user.authorized_projects
22
        present @projects, with: Entities::Project
N
Nihad Abbasov 已提交
23 24 25 26 27
      end

      # Get a single project
      #
      # Parameters:
28
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
29 30 31
      # Example Request:
      #   GET /projects/:id
      get ":id" do
32
        present user_project, with: Entities::Project
N
Nihad Abbasov 已提交
33 34
      end

35 36 37 38
      # Create new project
      #
      # Parameters:
      #   name (required) - name for new project
39 40 41 42 43 44
      #   description (optional) - short project description
      #   default_branch (optional) - 'master' by default
      #   issues_enabled (optional) - enabled by default
      #   wall_enabled (optional) - enabled by default
      #   merge_requests_enabled (optional) - enabled by default
      #   wiki_enabled (optional) - enabled by default
45
      #   namespace_id (optional) - defaults to user namespace
46 47 48
      # Example Request
      #   POST /projects
      post do
49
        required_attributes! [:name]
50
        attrs = attributes_for_keys [:name,
51 52 53 54 55
                                    :description,
                                    :default_branch,
                                    :issues_enabled,
                                    :wall_enabled,
                                    :merge_requests_enabled,
56 57
                                    :wiki_enabled,
                                    :namespace_id]
58
        @project = ::Projects::CreateContext.new(current_user, attrs).execute
59 60 61
        if @project.saved?
          present @project, with: Entities::Project
        else
62 63 64
          if @project.errors[:limit_reached].present?
            error!(@project.errors[:limit_reached], 403)
          end
65
          not_found!
66 67 68
        end
      end

A
Angus MacArthur 已提交
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
      # Create new project for a specified user.  Only available to admin users.
      #
      # Parameters:
      #   user_id (required) - The ID of a user
      #   name (required) - name for new project
      #   description (optional) - short project description
      #   default_branch (optional) - 'master' by default
      #   issues_enabled (optional) - enabled by default
      #   wall_enabled (optional) - enabled by default
      #   merge_requests_enabled (optional) - enabled by default
      #   wiki_enabled (optional) - enabled by default
      # Example Request
      #   POST /projects/user/:user_id
      post "user/:user_id" do
        authenticated_as_admin!
        user = User.find(params[:user_id])
        attrs = attributes_for_keys [:name,
                                    :description,
                                    :default_branch,
                                    :issues_enabled,
                                    :wall_enabled,
                                    :merge_requests_enabled,
                                    :wiki_enabled]
        @project = ::Projects::CreateContext.new(user, attrs).execute
        if @project.saved?
          present @project, with: Entities::Project
        else
          not_found!
        end
      end


N
Nihad Abbasov 已提交
101
      # Get a project team members
M
miks 已提交
102 103
      #
      # Parameters:
104
      #   id (required) - The ID of a project
V
Valeriy Sizov 已提交
105
      #   query         - Query string
M
miks 已提交
106
      # Example Request:
N
Nihad Abbasov 已提交
107 108
      #   GET /projects/:id/members
      get ":id/members" do
V
Valeriy Sizov 已提交
109 110 111 112 113
        if params[:query].present?
          @members = paginate user_project.users.where("username LIKE ?", "%#{params[:query]}%")
        else
          @members = paginate user_project.users
        end
N
Nihad Abbasov 已提交
114
        present @members, with: Entities::ProjectMember, project: user_project
M
miks 已提交
115 116
      end

N
Nihad Abbasov 已提交
117
      # Get a project team members
118 119
      #
      # Parameters:
120
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
121
      #   user_id (required) - The ID of a user
122
      # Example Request:
N
Nihad Abbasov 已提交
123 124 125 126 127 128 129 130 131
      #   GET /projects/:id/members/:user_id
      get ":id/members/:user_id" do
        @member = user_project.users.find params[:user_id]
        present @member, with: Entities::ProjectMember, project: user_project
      end

      # Add a new project team member
      #
      # Parameters:
132
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
133 134 135 136 137
      #   user_id (required) - The ID of a user
      #   access_level (required) - Project access level
      # Example Request:
      #   POST /projects/:id/members
      post ":id/members" do
R
randx 已提交
138
        authorize! :admin_project, user_project
139
        required_attributes! [:user_id, :access_level]
140 141 142 143 144 145 146 147 148 149 150 151

        # either the user is already a team member or a new one
        team_member = user_project.team_member_by_id(params[:user_id])
        if team_member.nil?
          team_member = user_project.users_projects.new(
            user_id: params[:user_id],
            project_access: params[:access_level]
          )
        end

        if team_member.save
          @member = team_member.user
N
Nihad Abbasov 已提交
152 153
          present @member, with: Entities::ProjectMember, project: user_project
        else
154
          handle_project_member_errors team_member.errors
N
Nihad Abbasov 已提交
155
        end
156 157
      end

N
Nihad Abbasov 已提交
158
      # Update project team member
M
miks 已提交
159 160
      #
      # Parameters:
161
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
162 163
      #   user_id (required) - The ID of a team member
      #   access_level (required) - Project access level
M
miks 已提交
164
      # Example Request:
N
Nihad Abbasov 已提交
165 166
      #   PUT /projects/:id/members/:user_id
      put ":id/members/:user_id" do
R
randx 已提交
167
        authorize! :admin_project, user_project
168
        required_attributes! [:access_level]
N
Nihad Abbasov 已提交
169

170
        team_member = user_project.users_projects.find_by_user_id(params[:user_id])
171
        not_found!("User can not be found") if team_member.nil?
172 173 174

        if team_member.update_attributes(project_access: params[:access_level])
          @member = team_member.user
N
Nihad Abbasov 已提交
175 176
          present @member, with: Entities::ProjectMember, project: user_project
        else
177
          handle_project_member_errors team_member.errors
N
Nihad Abbasov 已提交
178
        end
M
miks 已提交
179 180
      end

N
Nihad Abbasov 已提交
181
      # Remove a team member from project
M
miks 已提交
182 183
      #
      # Parameters:
184
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
185
      #   user_id (required) - The ID of a team member
M
miks 已提交
186
      # Example Request:
N
Nihad Abbasov 已提交
187 188
      #   DELETE /projects/:id/members/:user_id
      delete ":id/members/:user_id" do
R
randx 已提交
189
        authorize! :admin_project, user_project
190 191 192
        team_member = user_project.users_projects.find_by_user_id(params[:user_id])
        unless team_member.nil?
          team_member.destroy
193
        else
194
          {message: "Access revoked", id: params[:user_id].to_i}
195
        end
M
miks 已提交
196 197
      end

M
miks 已提交
198 199 200
      # Get project hooks
      #
      # Parameters:
201
      #   id (required) - The ID of a project
M
miks 已提交
202 203 204
      # Example Request:
      #   GET /projects/:id/hooks
      get ":id/hooks" do
M
miks 已提交
205
        authorize! :admin_project, user_project
M
miks 已提交
206 207 208
        @hooks = paginate user_project.hooks
        present @hooks, with: Entities::Hook
      end
S
Saito 已提交
209

J
jozefvaclavik 已提交
210 211 212
      # Get a project hook
      #
      # Parameters:
213
      #   id (required) - The ID of a project
J
jozefvaclavik 已提交
214 215 216 217
      #   hook_id (required) - The ID of a project hook
      # Example Request:
      #   GET /projects/:id/hooks/:hook_id
      get ":id/hooks/:hook_id" do
218
        authorize! :admin_project, user_project
J
jozefvaclavik 已提交
219 220 221
        @hook = user_project.hooks.find(params[:hook_id])
        present @hook, with: Entities::Hook
      end
S
Saito 已提交
222

M
miks 已提交
223 224 225 226

      # Add hook to project
      #
      # Parameters:
227
      #   id (required) - The ID of a project
M
miks 已提交
228 229 230 231
      #   url (required) - The hook URL
      # Example Request:
      #   POST /projects/:id/hooks
      post ":id/hooks" do
M
miks 已提交
232
        authorize! :admin_project, user_project
233
        required_attributes! [:url]
234

M
miks 已提交
235 236 237 238
        @hook = user_project.hooks.new({"url" => params[:url]})
        if @hook.save
          present @hook, with: Entities::Hook
        else
239 240 241
          if @hook.errors[:url].present?
            error!("Invalid url given", 422)
          end
242
          not_found!
M
miks 已提交
243 244
        end
      end
S
Saito 已提交
245

J
jozefvaclavik 已提交
246 247 248
      # Update an existing project hook
      #
      # Parameters:
249
      #   id (required) - The ID of a project
J
jozefvaclavik 已提交
250 251 252 253 254 255 256
      #   hook_id (required) - The ID of a project hook
      #   url (required) - The hook URL
      # Example Request:
      #   PUT /projects/:id/hooks/:hook_id
      put ":id/hooks/:hook_id" do
        @hook = user_project.hooks.find(params[:hook_id])
        authorize! :admin_project, user_project
257
        required_attributes! [:url]
J
jozefvaclavik 已提交
258

259
        attrs = attributes_for_keys [:url]
J
jozefvaclavik 已提交
260 261 262
        if @hook.update_attributes attrs
          present @hook, with: Entities::Hook
        else
263 264 265
          if @hook.errors[:url].present?
            error!("Invalid url given", 422)
          end
J
jozefvaclavik 已提交
266 267 268
          not_found!
        end
      end
M
miks 已提交
269

270
      # Deletes project hook. This is an idempotent function.
M
miks 已提交
271 272
      #
      # Parameters:
273
      #   id (required) - The ID of a project
M
miks 已提交
274 275
      #   hook_id (required) - The ID of hook to delete
      # Example Request:
276
      #   DELETE /projects/:id/hooks/:hook_id
277
      delete ":id/hooks/:hook_id" do
M
miks 已提交
278
        authorize! :admin_project, user_project
279
        required_attributes! [:hook_id]
280 281 282 283 284

        begin
          @hook = ProjectHook.find(params[:hook_id])
          @hook.destroy
        rescue
285
          # ProjectHook can raise Error if hook_id not found
286
        end
M
miks 已提交
287 288
      end

N
Nihad Abbasov 已提交
289 290 291
      # Get a project snippets
      #
      # Parameters:
292
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
293 294 295 296 297 298
      # Example Request:
      #   GET /projects/:id/snippets
      get ":id/snippets" do
        present paginate(user_project.snippets), with: Entities::ProjectSnippet
      end

N
Nihad Abbasov 已提交
299 300 301
      # Get a project snippet
      #
      # Parameters:
302
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
303 304 305 306
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   GET /projects/:id/snippets/:snippet_id
      get ":id/snippets/:snippet_id" do
N
Nihad Abbasov 已提交
307
        @snippet = user_project.snippets.find(params[:snippet_id])
308
        present @snippet, with: Entities::ProjectSnippet
N
Nihad Abbasov 已提交
309 310 311 312 313
      end

      # Create a new project snippet
      #
      # Parameters:
314
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
315 316 317 318 319 320 321
      #   title (required) - The title of a snippet
      #   file_name (required) - The name of a snippet file
      #   lifetime (optional) - The expiration date of a snippet
      #   code (required) - The content of a snippet
      # Example Request:
      #   POST /projects/:id/snippets
      post ":id/snippets" do
322
        authorize! :write_snippet, user_project
323
        required_attributes! [:title, :file_name, :code]
324

A
Alex Denisov 已提交
325
        attrs = attributes_for_keys [:title, :file_name]
A
Alex Denisov 已提交
326 327 328
        attrs[:expires_at] = params[:lifetime] if params[:lifetime].present?
        attrs[:content] = params[:code] if params[:code].present?
        @snippet = user_project.snippets.new attrs
N
Nihad Abbasov 已提交
329 330 331
        @snippet.author = current_user

        if @snippet.save
332
          present @snippet, with: Entities::ProjectSnippet
N
Nihad Abbasov 已提交
333
        else
334
          not_found!
N
Nihad Abbasov 已提交
335 336 337
        end
      end

338 339 340
      # Update an existing project snippet
      #
      # Parameters:
341
      #   id (required) - The ID of a project
342 343 344 345 346 347 348 349
      #   snippet_id (required) - The ID of a project snippet
      #   title (optional) - The title of a snippet
      #   file_name (optional) - The name of a snippet file
      #   lifetime (optional) - The expiration date of a snippet
      #   code (optional) - The content of a snippet
      # Example Request:
      #   PUT /projects/:id/snippets/:snippet_id
      put ":id/snippets/:snippet_id" do
N
Nihad Abbasov 已提交
350
        @snippet = user_project.snippets.find(params[:snippet_id])
R
randx 已提交
351 352
        authorize! :modify_snippet, @snippet

A
Alex Denisov 已提交
353
        attrs = attributes_for_keys [:title, :file_name]
A
Alex Denisov 已提交
354 355
        attrs[:expires_at] = params[:lifetime] if params[:lifetime].present?
        attrs[:content] = params[:code] if params[:code].present?
356

A
Alex Denisov 已提交
357
        if @snippet.update_attributes attrs
358
          present @snippet, with: Entities::ProjectSnippet
359
        else
360
          not_found!
361 362 363
        end
      end

N
Nihad Abbasov 已提交
364 365 366
      # Delete a project snippet
      #
      # Parameters:
367
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
368 369 370 371
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   DELETE /projects/:id/snippets/:snippet_id
      delete ":id/snippets/:snippet_id" do
372 373 374 375 376 377
        begin
          @snippet = user_project.snippets.find(params[:snippet_id])
          authorize! :modify_snippet, user_project
          @snippet.destroy
        rescue
        end
N
Nihad Abbasov 已提交
378
      end
379 380 381 382

      # Get a raw project snippet
      #
      # Parameters:
383
      #   id (required) - The ID of a project
384 385 386 387
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   GET /projects/:id/snippets/:snippet_id/raw
      get ":id/snippets/:snippet_id/raw" do
N
Nihad Abbasov 已提交
388
        @snippet = user_project.snippets.find(params[:snippet_id])
389
        content_type 'text/plain'
390 391
        present @snippet.content
      end
392

M
Matt Humphrey 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
      # Get a specific project's keys
      #
      # Example Request:
      #   GET /projects/:id/keys
      get ":id/keys" do
        present user_project.deploy_keys, with: Entities::SSHKey
      end

      # Get single key owned by currently authenticated user
      #
      # Example Request:
      #   GET /projects/:id/keys/:id
      get ":id/keys/:key_id" do
        key = user_project.deploy_keys.find params[:key_id]
        present key, with: Entities::SSHKey
      end

      # Add new ssh key to currently authenticated user
      #
      # Parameters:
      #   key (required) - New SSH Key
      #   title (required) - New SSH Key's title
      # Example Request:
      #   POST /projects/:id/keys
      post ":id/keys" do
        attrs = attributes_for_keys [:title, :key]
419 420
        key = DeployKey.new attrs
        if key.valid? && user_project.deploy_keys << key
M
Matt Humphrey 已提交
421 422 423 424 425 426 427 428 429 430 431 432
          present key, with: Entities::SSHKey
        else
          not_found!
        end
      end

      # Delete existed ssh key of currently authenticated user
      #
      # Example Request:
      #   DELETE /projects/:id/keys/:id
      delete ":id/keys/:key_id" do
        key = user_project.deploy_keys.find params[:key_id]
433
        key.destroy
M
Matt Humphrey 已提交
434
      end
N
Nihad Abbasov 已提交
435 436 437
    end
  end
end