projects.rb 15.4 KB
Newer Older
N
Nihad Abbasov 已提交
1 2 3 4 5 6
module Gitlab
  # 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 46 47
      # Example Request
      #   POST /projects
      post do
48
        required_attributes! [:name]
49
        attrs = attributes_for_keys [:name,
50 51 52 53 54
                                    :description,
                                    :default_branch,
                                    :issues_enabled,
                                    :wall_enabled,
                                    :merge_requests_enabled,
S
Sebastian Ziebell 已提交
55
                                    :wiki_enabled]
56
        @project = ::Projects::CreateContext.new(current_user, attrs).execute
57 58 59
        if @project.saved?
          present @project, with: Entities::Project
        else
60 61 62
          if @project.errors[:limit_reached].present?
            error!(@project.errors[:limit_reached], 403)
          end
63
          not_found!
64 65 66
        end
      end

N
Nihad Abbasov 已提交
67
      # Get a project team members
M
miks 已提交
68 69
      #
      # Parameters:
70
      #   id (required) - The ID of a project
V
Valeriy Sizov 已提交
71
      #   query         - Query string
M
miks 已提交
72
      # Example Request:
N
Nihad Abbasov 已提交
73 74
      #   GET /projects/:id/members
      get ":id/members" do
V
Valeriy Sizov 已提交
75 76 77 78 79
        if params[:query].present?
          @members = paginate user_project.users.where("username LIKE ?", "%#{params[:query]}%")
        else
          @members = paginate user_project.users
        end
N
Nihad Abbasov 已提交
80
        present @members, with: Entities::ProjectMember, project: user_project
M
miks 已提交
81 82
      end

N
Nihad Abbasov 已提交
83
      # Get a project team members
84 85
      #
      # Parameters:
86
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
87
      #   user_id (required) - The ID of a user
88
      # Example Request:
N
Nihad Abbasov 已提交
89 90 91 92 93 94 95 96 97
      #   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:
98
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
99 100 101 102 103
      #   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 已提交
104
        authorize! :admin_project, user_project
105
        required_attributes! [:user_id, :access_level]
106 107 108 109 110 111 112 113 114 115 116 117

        # 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 已提交
118 119
          present @member, with: Entities::ProjectMember, project: user_project
        else
120
          handle_project_member_errors team_member.errors
N
Nihad Abbasov 已提交
121
        end
122 123
      end

N
Nihad Abbasov 已提交
124
      # Update project team member
M
miks 已提交
125 126
      #
      # Parameters:
127
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
128 129
      #   user_id (required) - The ID of a team member
      #   access_level (required) - Project access level
M
miks 已提交
130
      # Example Request:
N
Nihad Abbasov 已提交
131 132
      #   PUT /projects/:id/members/:user_id
      put ":id/members/:user_id" do
R
randx 已提交
133
        authorize! :admin_project, user_project
134
        required_attributes! [:access_level]
N
Nihad Abbasov 已提交
135

136
        team_member = user_project.users_projects.find_by_user_id(params[:user_id])
137
        not_found!("User can not be found") if team_member.nil?
138 139 140

        if team_member.update_attributes(project_access: params[:access_level])
          @member = team_member.user
N
Nihad Abbasov 已提交
141 142
          present @member, with: Entities::ProjectMember, project: user_project
        else
143
          handle_project_member_errors team_member.errors
N
Nihad Abbasov 已提交
144
        end
M
miks 已提交
145 146
      end

N
Nihad Abbasov 已提交
147
      # Remove a team member from project
M
miks 已提交
148 149
      #
      # Parameters:
150
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
151
      #   user_id (required) - The ID of a team member
M
miks 已提交
152
      # Example Request:
N
Nihad Abbasov 已提交
153 154
      #   DELETE /projects/:id/members/:user_id
      delete ":id/members/:user_id" do
R
randx 已提交
155
        authorize! :admin_project, user_project
156 157 158
        team_member = user_project.users_projects.find_by_user_id(params[:user_id])
        unless team_member.nil?
          team_member.destroy
159 160 161
        else
          {:message => "Access revoked", :id => params[:user_id].to_i}
        end
M
miks 已提交
162 163
      end

M
miks 已提交
164 165 166
      # Get project hooks
      #
      # Parameters:
167
      #   id (required) - The ID of a project
M
miks 已提交
168 169 170
      # Example Request:
      #   GET /projects/:id/hooks
      get ":id/hooks" do
M
miks 已提交
171
        authorize! :admin_project, user_project
M
miks 已提交
172 173 174
        @hooks = paginate user_project.hooks
        present @hooks, with: Entities::Hook
      end
S
Saito 已提交
175

J
jozefvaclavik 已提交
176 177 178
      # Get a project hook
      #
      # Parameters:
179
      #   id (required) - The ID of a project
J
jozefvaclavik 已提交
180 181 182 183
      #   hook_id (required) - The ID of a project hook
      # Example Request:
      #   GET /projects/:id/hooks/:hook_id
      get ":id/hooks/:hook_id" do
184
        authorize! :admin_project, user_project
J
jozefvaclavik 已提交
185 186 187
        @hook = user_project.hooks.find(params[:hook_id])
        present @hook, with: Entities::Hook
      end
S
Saito 已提交
188

M
miks 已提交
189 190 191 192

      # Add hook to project
      #
      # Parameters:
193
      #   id (required) - The ID of a project
M
miks 已提交
194 195 196 197
      #   url (required) - The hook URL
      # Example Request:
      #   POST /projects/:id/hooks
      post ":id/hooks" do
M
miks 已提交
198
        authorize! :admin_project, user_project
199
        required_attributes! [:url]
200

M
miks 已提交
201 202 203 204
        @hook = user_project.hooks.new({"url" => params[:url]})
        if @hook.save
          present @hook, with: Entities::Hook
        else
205 206 207
          if @hook.errors[:url].present?
            error!("Invalid url given", 422)
          end
208
          not_found!
M
miks 已提交
209 210
        end
      end
S
Saito 已提交
211

J
jozefvaclavik 已提交
212 213 214
      # Update an existing project hook
      #
      # Parameters:
215
      #   id (required) - The ID of a project
J
jozefvaclavik 已提交
216 217 218 219 220 221 222
      #   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
223
        required_attributes! [:url]
J
jozefvaclavik 已提交
224

225
        attrs = attributes_for_keys [:url]
J
jozefvaclavik 已提交
226 227 228
        if @hook.update_attributes attrs
          present @hook, with: Entities::Hook
        else
229 230 231
          if @hook.errors[:url].present?
            error!("Invalid url given", 422)
          end
J
jozefvaclavik 已提交
232 233 234
          not_found!
        end
      end
M
miks 已提交
235 236 237 238

      # Delete project hook
      #
      # Parameters:
239
      #   id (required) - The ID of a project
M
miks 已提交
240 241
      #   hook_id (required) - The ID of hook to delete
      # Example Request:
242
      #   DELETE /projects/:id/hooks/:hook_id
243
      delete ":id/hooks" do
M
miks 已提交
244
        authorize! :admin_project, user_project
245
        required_attributes! [:hook_id]
246 247 248 249 250 251

        begin
          @hook = ProjectHook.find(params[:hook_id])
          @hook.destroy
        rescue
        end
M
miks 已提交
252 253
      end

N
Nihad Abbasov 已提交
254 255 256
      # Get a project repository branches
      #
      # Parameters:
257
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
258 259 260
      # Example Request:
      #   GET /projects/:id/repository/branches
      get ":id/repository/branches" do
261
        present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject, project: user_project
N
Nihad Abbasov 已提交
262 263
      end

264 265 266
      # Get a single branch
      #
      # Parameters:
267
      #   id (required) - The ID of a project
268
      #   branch (required) - The name of the branch
269
      # Example Request:
270 271 272
      #   GET /projects/:id/repository/branches/:branch
      get ":id/repository/branches/:branch" do
        @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
273
        not_found!("Branch does not exist") if @branch.nil?
274 275 276 277 278 279 280 281 282 283 284 285
        present @branch, with: Entities::RepoObject, project: user_project
      end

      # Protect a single branch
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   branch (required) - The name of the branch
      # Example Request:
      #   PUT /projects/:id/repository/branches/:branch/protect
      put ":id/repository/branches/:branch/protect" do
        @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
286
        not_found! unless @branch
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        protected = user_project.protected_branches.find_by_name(@branch.name)

        unless protected
          user_project.protected_branches.create(:name => @branch.name)
        end

        present @branch, with: Entities::RepoObject, project: user_project
      end

      # Unprotect a single branch
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   branch (required) - The name of the branch
      # Example Request:
      #   PUT /projects/:id/repository/branches/:branch/unprotect
      put ":id/repository/branches/:branch/unprotect" do
        @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
305
        not_found! unless @branch
306 307 308 309 310 311 312
        protected = user_project.protected_branches.find_by_name(@branch.name)

        if protected
          protected.destroy
        end

        present @branch, with: Entities::RepoObject, project: user_project
313 314
      end

N
Nihad Abbasov 已提交
315 316 317
      # Get a project repository tags
      #
      # Parameters:
318
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
319 320 321
      # Example Request:
      #   GET /projects/:id/repository/tags
      get ":id/repository/tags" do
322
        present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject
N
Nihad Abbasov 已提交
323
      end
N
Nihad Abbasov 已提交
324

325 326 327
      # Get a project repository commits
      #
      # Parameters:
328
      #   id (required) - The ID of a project
329 330 331
      #   ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used
      #   page (optional) - The page number of the commit pagination
      #   per_page (optional) - The number of elements per page used in pagination
332 333 334 335 336 337
      # Example Request:
      #   GET /projects/:id/repository/commits
      get ":id/repository/commits" do
        authorize! :download_code, user_project

        page = params[:page] || 0
N
Nihad Abbasov 已提交
338
        per_page = (params[:per_page] || 20).to_i
339 340
        ref = params[:ref_name] || user_project.try(:default_branch) || 'master'

D
Dmitriy Zaporozhets 已提交
341
        commits = user_project.repository.commits(ref, nil, per_page, page * per_page)
342 343 344
        present CommitDecorator.decorate(commits), with: Entities::RepoCommit
      end

N
Nihad Abbasov 已提交
345 346 347
      # Get a project snippets
      #
      # Parameters:
348
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
349 350 351 352 353 354
      # Example Request:
      #   GET /projects/:id/snippets
      get ":id/snippets" do
        present paginate(user_project.snippets), with: Entities::ProjectSnippet
      end

N
Nihad Abbasov 已提交
355 356 357
      # Get a project snippet
      #
      # Parameters:
358
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
359 360 361 362
      #   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 已提交
363
        @snippet = user_project.snippets.find(params[:snippet_id])
364
        present @snippet, with: Entities::ProjectSnippet
N
Nihad Abbasov 已提交
365 366 367 368 369
      end

      # Create a new project snippet
      #
      # Parameters:
370
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
371 372 373 374 375 376 377
      #   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
378
        authorize! :write_snippet, user_project
379
        required_attributes! [:title, :file_name, :code]
380

A
Alex Denisov 已提交
381
        attrs = attributes_for_keys [:title, :file_name]
A
Alex Denisov 已提交
382 383 384
        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 已提交
385 386 387
        @snippet.author = current_user

        if @snippet.save
388
          present @snippet, with: Entities::ProjectSnippet
N
Nihad Abbasov 已提交
389
        else
390
          not_found!
N
Nihad Abbasov 已提交
391 392 393
        end
      end

394 395 396
      # Update an existing project snippet
      #
      # Parameters:
397
      #   id (required) - The ID of a project
398 399 400 401 402 403 404 405
      #   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 已提交
406
        @snippet = user_project.snippets.find(params[:snippet_id])
R
randx 已提交
407 408
        authorize! :modify_snippet, @snippet

A
Alex Denisov 已提交
409
        attrs = attributes_for_keys [:title, :file_name]
A
Alex Denisov 已提交
410 411
        attrs[:expires_at] = params[:lifetime] if params[:lifetime].present?
        attrs[:content] = params[:code] if params[:code].present?
412

A
Alex Denisov 已提交
413
        if @snippet.update_attributes attrs
414
          present @snippet, with: Entities::ProjectSnippet
415
        else
416
          not_found!
417 418 419
        end
      end

N
Nihad Abbasov 已提交
420 421 422
      # Delete a project snippet
      #
      # Parameters:
423
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
424 425 426 427
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   DELETE /projects/:id/snippets/:snippet_id
      delete ":id/snippets/:snippet_id" do
428 429 430 431 432 433
        begin
          @snippet = user_project.snippets.find(params[:snippet_id])
          authorize! :modify_snippet, user_project
          @snippet.destroy
        rescue
        end
N
Nihad Abbasov 已提交
434
      end
435 436 437 438

      # Get a raw project snippet
      #
      # Parameters:
439
      #   id (required) - The ID of a project
440 441 442 443
      #   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 已提交
444
        @snippet = user_project.snippets.find(params[:snippet_id])
445
        content_type 'text/plain'
446 447
        present @snippet.content
      end
448 449 450 451

      # Get a raw file contents
      #
      # Parameters:
452
      #   id (required) - The ID of a project
453
      #   sha (required) - The commit or branch name
454 455 456 457
      #   filepath (required) - The path to the file to display
      # Example Request:
      #   GET /projects/:id/repository/commits/:sha/blob
      get ":id/repository/commits/:sha/blob" do
458
        authorize! :download_code, user_project
459
        required_attributes! [:filepath]
460

461 462
        ref = params[:sha]

D
Dmitriy Zaporozhets 已提交
463
        commit = user_project.repository.commit ref
464
        not_found! "Commit" unless commit
465

D
Dmitriy Zaporozhets 已提交
466
        tree = Tree.new commit.tree, ref, params[:filepath]
467
        not_found! "File" unless tree.try(:tree)
468

S
Saito 已提交
469
        content_type tree.mime_type
470 471 472
        present tree.data
      end

N
Nihad Abbasov 已提交
473 474 475
    end
  end
end