projects.rb 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
module Gitlab
  # Projects API
  class Projects < Grape::API
    before { authenticate! }

    resource :projects do
      # Get a projects list for authenticated user
      #
      # Example Request:
      #   GET /projects
      get do
12
        @projects = paginate current_user.authorized_projects
13
        present @projects, with: Entities::Project
14 15 16 17 18
      end

      # Get a single project
      #
      # Parameters:
19
      #   id (required) - The ID of a project
20 21 22
      # Example Request:
      #   GET /projects/:id
      get ":id" do
23
        present user_project, with: Entities::Project
24 25
      end

26 27 28 29
      # Create new project
      #
      # Parameters:
      #   name (required) - name for new project
30 31 32 33 34 35
      #   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
36 37 38
      # Example Request
      #   POST /projects
      post do
39
        attrs = attributes_for_keys [:name,
40 41 42 43 44
                                    :description,
                                    :default_branch,
                                    :issues_enabled,
                                    :wall_enabled,
                                    :merge_requests_enabled,
Alex Denisov's avatar
Alex Denisov committed
45
                                    :wiki_enabled]
46
        @project = ::Projects::CreateContext.new(current_user, attrs).execute
47 48 49
        if @project.saved?
          present @project, with: Entities::Project
        else
50
          not_found!
51 52 53
        end
      end

54
      # Get a project team members
miks's avatar
miks committed
55 56
      #
      # Parameters:
57
      #   id (required) - The ID of a project
Valeriy Sizov's avatar
Valeriy Sizov committed
58
      #   query         - Query string
miks's avatar
miks committed
59
      # Example Request:
60 61
      #   GET /projects/:id/members
      get ":id/members" do
Valeriy Sizov's avatar
Valeriy Sizov committed
62 63 64 65 66
        if params[:query].present?
          @members = paginate user_project.users.where("username LIKE ?", "%#{params[:query]}%")
        else
          @members = paginate user_project.users
        end
67
        present @members, with: Entities::ProjectMember, project: user_project
miks's avatar
miks committed
68 69
      end

70
      # Get a project team members
71 72
      #
      # Parameters:
73
      #   id (required) - The ID of a project
74
      #   user_id (required) - The ID of a user
75
      # Example Request:
76 77 78 79 80 81 82 83 84
      #   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:
85
      #   id (required) - The ID of a project
86 87 88 89 90
      #   user_id (required) - The ID of a user
      #   access_level (required) - Project access level
      # Example Request:
      #   POST /projects/:id/members
      post ":id/members" do
randx's avatar
randx committed
91
        authorize! :admin_project, user_project
92 93 94 95 96 97 98 99 100 101 102
        users_project = user_project.users_projects.new(
          user_id: params[:user_id],
          project_access: params[:access_level]
        )

        if users_project.save
          @member = users_project.user
          present @member, with: Entities::ProjectMember, project: user_project
        else
          not_found!
        end
103 104
      end

105
      # Update project team member
miks's avatar
miks committed
106 107
      #
      # Parameters:
108
      #   id (required) - The ID of a project
109 110
      #   user_id (required) - The ID of a team member
      #   access_level (required) - Project access level
miks's avatar
miks committed
111
      # Example Request:
112 113
      #   PUT /projects/:id/members/:user_id
      put ":id/members/:user_id" do
randx's avatar
randx committed
114
        authorize! :admin_project, user_project
115 116 117 118 119 120 121 122
        users_project = user_project.users_projects.find_by_user_id params[:user_id]

        if users_project.update_attributes(project_access: params[:access_level])
          @member = users_project.user
          present @member, with: Entities::ProjectMember, project: user_project
        else
          not_found!
        end
miks's avatar
miks committed
123 124
      end

125
      # Remove a team member from project
miks's avatar
miks committed
126 127
      #
      # Parameters:
128
      #   id (required) - The ID of a project
129
      #   user_id (required) - The ID of a team member
miks's avatar
miks committed
130
      # Example Request:
131 132
      #   DELETE /projects/:id/members/:user_id
      delete ":id/members/:user_id" do
randx's avatar
randx committed
133
        authorize! :admin_project, user_project
134 135
        users_project = user_project.users_projects.find_by_user_id params[:user_id]
        users_project.destroy
miks's avatar
miks committed
136 137
      end

miks's avatar
miks committed
138 139 140
      # Get project hooks
      #
      # Parameters:
141
      #   id (required) - The ID of a project
miks's avatar
miks committed
142 143 144
      # Example Request:
      #   GET /projects/:id/hooks
      get ":id/hooks" do
miks's avatar
miks committed
145
        authorize! :admin_project, user_project
miks's avatar
miks committed
146 147 148
        @hooks = paginate user_project.hooks
        present @hooks, with: Entities::Hook
      end
Saito's avatar
Saito committed
149

jozefvaclavik's avatar
jozefvaclavik committed
150 151 152
      # Get a project hook
      #
      # Parameters:
153
      #   id (required) - The ID of a project
jozefvaclavik's avatar
jozefvaclavik committed
154 155 156 157 158 159 160
      #   hook_id (required) - The ID of a project hook
      # Example Request:
      #   GET /projects/:id/hooks/:hook_id
      get ":id/hooks/:hook_id" do
        @hook = user_project.hooks.find(params[:hook_id])
        present @hook, with: Entities::Hook
      end
Saito's avatar
Saito committed
161

miks's avatar
miks committed
162 163 164 165

      # Add hook to project
      #
      # Parameters:
166
      #   id (required) - The ID of a project
miks's avatar
miks committed
167 168 169 170
      #   url (required) - The hook URL
      # Example Request:
      #   POST /projects/:id/hooks
      post ":id/hooks" do
miks's avatar
miks committed
171
        authorize! :admin_project, user_project
miks's avatar
miks committed
172 173 174 175 176 177 178
        @hook = user_project.hooks.new({"url" => params[:url]})
        if @hook.save
          present @hook, with: Entities::Hook
        else
          error!({'message' => '404 Not found'}, 404)
        end
      end
Saito's avatar
Saito committed
179

jozefvaclavik's avatar
jozefvaclavik committed
180 181 182
      # Update an existing project hook
      #
      # Parameters:
183
      #   id (required) - The ID of a project
jozefvaclavik's avatar
jozefvaclavik committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
      #   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

        attrs = attributes_for_keys [:url]

        if @hook.update_attributes attrs
          present @hook, with: Entities::Hook
        else
          not_found!
        end
      end
miks's avatar
miks committed
200 201 202 203

      # Delete project hook
      #
      # Parameters:
204
      #   id (required) - The ID of a project
miks's avatar
miks committed
205 206 207 208
      #   hook_id (required) - The ID of hook to delete
      # Example Request:
      #   DELETE /projects/:id/hooks
      delete ":id/hooks" do
miks's avatar
miks committed
209
        authorize! :admin_project, user_project
miks's avatar
miks committed
210 211 212 213
        @hook = user_project.hooks.find(params[:hook_id])
        @hook.destroy
      end

214 215 216
      # Get a project repository branches
      #
      # Parameters:
217
      #   id (required) - The ID of a project
218 219 220
      # Example Request:
      #   GET /projects/:id/repository/branches
      get ":id/repository/branches" do
221
        present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject
222 223
      end

224 225 226
      # Get a single branch
      #
      # Parameters:
227
      #   id (required) - The ID of a project
228
      #   branch (required) - The name of the branch
229
      # Example Request:
230 231 232
      #   GET /projects/:id/repository/branches/:branch
      get ":id/repository/branches/:branch" do
        @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
233
        present @branch, with: Entities::RepoObject
234 235
      end

236 237 238
      # Get a project repository tags
      #
      # Parameters:
239
      #   id (required) - The ID of a project
240 241 242
      # Example Request:
      #   GET /projects/:id/repository/tags
      get ":id/repository/tags" do
243
        present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject
244
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
245

246 247 248
      # Get a project repository commits
      #
      # Parameters:
249
      #   id (required) - The ID of a project
250 251 252 253 254 255 256 257 258 259
      #   ref_name (optional) - The name of a repository branch or tag
      # Example Request:
      #   GET /projects/:id/repository/commits
      get ":id/repository/commits" do
        authorize! :download_code, user_project

        page = params[:page] || 0
        per_page = params[:per_page] || 20
        ref = params[:ref_name] || user_project.try(:default_branch) || 'master'

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
260
        commits = user_project.repository.commits(ref, nil, per_page, page * per_page)
261 262 263
        present CommitDecorator.decorate(commits), with: Entities::RepoCommit
      end

264 265 266
      # Get a project snippets
      #
      # Parameters:
267
      #   id (required) - The ID of a project
268 269 270 271 272 273
      # Example Request:
      #   GET /projects/:id/snippets
      get ":id/snippets" do
        present paginate(user_project.snippets), with: Entities::ProjectSnippet
      end

Nihad Abbasov's avatar
Nihad Abbasov committed
274 275 276
      # Get a project snippet
      #
      # Parameters:
277
      #   id (required) - The ID of a project
Nihad Abbasov's avatar
Nihad Abbasov committed
278 279 280 281
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   GET /projects/:id/snippets/:snippet_id
      get ":id/snippets/:snippet_id" do
Nihad Abbasov's avatar
Nihad Abbasov committed
282
        @snippet = user_project.snippets.find(params[:snippet_id])
283
        present @snippet, with: Entities::ProjectSnippet
Nihad Abbasov's avatar
Nihad Abbasov committed
284 285 286 287 288
      end

      # Create a new project snippet
      #
      # Parameters:
289
      #   id (required) - The ID of a project
Nihad Abbasov's avatar
Nihad Abbasov committed
290 291 292 293 294 295 296
      #   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
297 298
        authorize! :write_snippet, user_project

Alex Denisov's avatar
Alex Denisov committed
299
        attrs = attributes_for_keys [:title, :file_name]
Alex Denisov's avatar
Alex Denisov committed
300 301 302
        attrs[:expires_at] = params[:lifetime] if params[:lifetime].present?
        attrs[:content] = params[:code] if params[:code].present?
        @snippet = user_project.snippets.new attrs
Nihad Abbasov's avatar
Nihad Abbasov committed
303 304 305
        @snippet.author = current_user

        if @snippet.save
306
          present @snippet, with: Entities::ProjectSnippet
Nihad Abbasov's avatar
Nihad Abbasov committed
307
        else
308
          not_found!
Nihad Abbasov's avatar
Nihad Abbasov committed
309 310 311
        end
      end

312 313 314
      # Update an existing project snippet
      #
      # Parameters:
315
      #   id (required) - The ID of a project
316 317 318 319 320 321 322 323
      #   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
Nihad Abbasov's avatar
Nihad Abbasov committed
324
        @snippet = user_project.snippets.find(params[:snippet_id])
randx's avatar
randx committed
325 326
        authorize! :modify_snippet, @snippet

Alex Denisov's avatar
Alex Denisov committed
327
        attrs = attributes_for_keys [:title, :file_name]
Alex Denisov's avatar
Alex Denisov committed
328 329
        attrs[:expires_at] = params[:lifetime] if params[:lifetime].present?
        attrs[:content] = params[:code] if params[:code].present?
330

Alex Denisov's avatar
Alex Denisov committed
331
        if @snippet.update_attributes attrs
332
          present @snippet, with: Entities::ProjectSnippet
333
        else
334
          not_found!
335 336 337
        end
      end

Nihad Abbasov's avatar
Nihad Abbasov committed
338 339 340
      # Delete a project snippet
      #
      # Parameters:
341
      #   id (required) - The ID of a project
Nihad Abbasov's avatar
Nihad Abbasov committed
342 343 344 345
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   DELETE /projects/:id/snippets/:snippet_id
      delete ":id/snippets/:snippet_id" do
Nihad Abbasov's avatar
Nihad Abbasov committed
346
        @snippet = user_project.snippets.find(params[:snippet_id])
randx's avatar
randx committed
347 348
        authorize! :modify_snippet, @snippet

Nihad Abbasov's avatar
Nihad Abbasov committed
349 350
        @snippet.destroy
      end
351 352 353 354

      # Get a raw project snippet
      #
      # Parameters:
355
      #   id (required) - The ID of a project
356 357 358 359
      #   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
Nihad Abbasov's avatar
Nihad Abbasov committed
360
        @snippet = user_project.snippets.find(params[:snippet_id])
361
        content_type 'text/plain'
362 363
        present @snippet.content
      end
364 365 366 367

      # Get a raw file contents
      #
      # Parameters:
368
      #   id (required) - The ID of a project
369
      #   sha (required) - The commit or branch name
370 371 372 373
      #   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
374 375
        authorize! :download_code, user_project

376 377
        ref = params[:sha]

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
378
        commit = user_project.repository.commit ref
379
        not_found! "Commit" unless commit
380

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
381
        tree = Tree.new commit.tree, ref, params[:filepath]
382
        not_found! "File" unless tree.try(:tree)
383

Saito's avatar
Saito committed
384
        content_type tree.mime_type
385 386 387
        present tree.data
      end

388 389 390
    end
  end
end