projects.rb 10.0 KB
Newer Older
N
Nihad Abbasov 已提交
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
N
Nihad Abbasov 已提交
12
        @projects = paginate current_user.projects
13
        present @projects, with: Entities::Project
N
Nihad Abbasov 已提交
14 15 16 17 18
      end

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

26 27 28 29
      # Create new project
      #
      # Parameters:
      #   name (required) - name for new project
A
Alex Denisov 已提交
30 31
      #   code (optional) - code for new project, uses project name if not set
      #   path (optional) - path for new project, uses project name if not set
32 33 34 35 36 37
      #   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
38 39 40
      # Example Request
      #   POST /projects
      post do
A
Alex Denisov 已提交
41 42
        params[:code] ||= params[:name]
        params[:path] ||= params[:name]
A
Alex Denisov 已提交
43
        attrs = attributes_for_keys [:code, 
A
Alex Denisov 已提交
44 45 46 47 48 49 50 51 52
                                    :path, 
                                    :name, 
                                    :description, 
                                    :default_branch, 
                                    :issues_enabled, 
                                    :wall_enabled, 
                                    :merge_requests_enabled, 
                                    :wiki_enabled]
        @project = Project.create_by_user(attrs, current_user)
53 54 55
        if @project.saved?
          present @project, with: Entities::Project
        else
56
          not_found!
57 58 59
        end
      end

M
miks 已提交
60 61 62 63 64 65 66 67 68 69 70
      # Get project users
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
      # Example Request:
      #   GET /projects/:id/users
      get ":id/users" do
        @users_projects = paginate user_project.users_projects
        present @users_projects, with: Entities::UsersProject
      end

71 72 73 74 75 76 77
      # Add users to project with specified access level
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
      #   user_ids (required) - The ID list of users to add
      #   project_access (required) - Project access level
      # Example Request:
M
miks 已提交
78 79
      #   POST /projects/:id/users
      post ":id/users" do
R
randx 已提交
80
        authorize! :admin_project, user_project
M
miks 已提交
81
        user_project.add_users_ids_to_team(params[:user_ids].values, params[:project_access])
M
miks 已提交
82
        nil
83 84
      end

M
miks 已提交
85 86 87 88 89 90 91 92 93
      # Update users to specified access level
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
      #   user_ids (required) - The ID list of users to add
      #   project_access (required) - New project access level to
      # Example Request:
      #   PUT /projects/:id/add_users
      put ":id/users" do
R
randx 已提交
94
        authorize! :admin_project, user_project
M
miks 已提交
95
        user_project.update_users_ids_to_role(params[:user_ids].values, params[:project_access])
M
miks 已提交
96
        nil
M
miks 已提交
97 98 99 100 101 102 103 104 105 106
      end

      # Delete project users
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
      #   user_ids (required) - The ID list of users to delete
      # Example Request:
      #   DELETE /projects/:id/users
      delete ":id/users" do
R
randx 已提交
107
        authorize! :admin_project, user_project
M
miks 已提交
108
        user_project.delete_users_ids_from_team(params[:user_ids].values)
M
miks 已提交
109
        nil
M
miks 已提交
110 111
      end

M
miks 已提交
112 113 114 115 116 117 118
      # Get project hooks
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
      # Example Request:
      #   GET /projects/:id/hooks
      get ":id/hooks" do
M
miks 已提交
119
        authorize! :admin_project, user_project
M
miks 已提交
120 121 122 123 124 125 126 127 128 129 130 131
        @hooks = paginate user_project.hooks
        present @hooks, with: Entities::Hook
      end

      # Add hook to project
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
      #   url (required) - The hook URL
      # Example Request:
      #   POST /projects/:id/hooks
      post ":id/hooks" do
M
miks 已提交
132
        authorize! :admin_project, user_project
M
miks 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        @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

      # Delete project hook
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
      #   hook_id (required) - The ID of hook to delete
      # Example Request:
      #   DELETE /projects/:id/hooks
      delete ":id/hooks" do
M
miks 已提交
149
        authorize! :admin_project, user_project
M
miks 已提交
150 151 152 153 154
        @hook = user_project.hooks.find(params[:hook_id])
        @hook.destroy
        nil
      end

N
Nihad Abbasov 已提交
155 156 157
      # Get a project repository branches
      #
      # Parameters:
N
Nihad Abbasov 已提交
158
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
159 160 161
      # Example Request:
      #   GET /projects/:id/repository/branches
      get ":id/repository/branches" do
162
        present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject
N
Nihad Abbasov 已提交
163 164
      end

165 166 167 168
      # Get a single branch
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
169
      #   branch (required) - The name of the branch
170
      # Example Request:
171 172 173
      #   GET /projects/:id/repository/branches/:branch
      get ":id/repository/branches/:branch" do
        @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
174
        present @branch, with: Entities::RepoObject
175 176
      end

N
Nihad Abbasov 已提交
177 178 179
      # Get a project repository tags
      #
      # Parameters:
N
Nihad Abbasov 已提交
180
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
181 182 183
      # Example Request:
      #   GET /projects/:id/repository/tags
      get ":id/repository/tags" do
184
        present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject
N
Nihad Abbasov 已提交
185
      end
N
Nihad Abbasov 已提交
186 187 188 189

      # Get a project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
190
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
191 192 193 194
      #   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 已提交
195
        @snippet = user_project.snippets.find(params[:snippet_id])
196
        present @snippet, with: Entities::ProjectSnippet
N
Nihad Abbasov 已提交
197 198 199 200 201
      end

      # Create a new project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
202
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
203 204 205 206 207 208 209
      #   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
A
Alex Denisov 已提交
210
        attrs = attributes_for_keys [:title, :file_name]
A
Alex Denisov 已提交
211 212 213
        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 已提交
214 215 216
        @snippet.author = current_user

        if @snippet.save
217
          present @snippet, with: Entities::ProjectSnippet
N
Nihad Abbasov 已提交
218
        else
219
          not_found!
N
Nihad Abbasov 已提交
220 221 222
        end
      end

223 224 225
      # Update an existing project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
226
      #   id (required) - The ID or code name of a project
227 228 229 230 231 232 233 234
      #   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 已提交
235
        @snippet = user_project.snippets.find(params[:snippet_id])
R
randx 已提交
236 237
        authorize! :modify_snippet, @snippet

A
Alex Denisov 已提交
238
        attrs = attributes_for_keys [:title, :file_name]
A
Alex Denisov 已提交
239 240
        attrs[:expires_at] = params[:lifetime] if params[:lifetime].present?
        attrs[:content] = params[:code] if params[:code].present?
241

A
Alex Denisov 已提交
242
        if @snippet.update_attributes attrs
243
          present @snippet, with: Entities::ProjectSnippet
244
        else
245
          not_found!
246 247 248
        end
      end

N
Nihad Abbasov 已提交
249 250 251
      # Delete a project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
252
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
253 254 255 256
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   DELETE /projects/:id/snippets/:snippet_id
      delete ":id/snippets/:snippet_id" do
N
Nihad Abbasov 已提交
257
        @snippet = user_project.snippets.find(params[:snippet_id])
R
randx 已提交
258 259
        authorize! :modify_snippet, @snippet

N
Nihad Abbasov 已提交
260 261
        @snippet.destroy
      end
262 263 264 265

      # Get a raw project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
266
      #   id (required) - The ID or code name of a project
267 268 269 270
      #   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 已提交
271
        @snippet = user_project.snippets.find(params[:snippet_id])
272
        content_type 'text/plain'
273 274
        present @snippet.content
      end
275 276 277 278 279

      # Get a raw file contents
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
280
      #   sha (required) - The commit or branch name
281 282 283 284 285 286 287
      #   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
        ref = params[:sha]

        commit = user_project.commit ref
288
        not_found! "Commit" unless commit
289

290
        tree = Tree.new commit.tree, user_project, ref, params[:filepath]
291
        not_found! "File" unless tree.try(:tree)
292

293 294 295 296 297 298 299 300 301 302
        if tree.text?
          encoding = Gitlab::Encode.detect_encoding(tree.data)
          content_type encoding ? "text/plain; charset=#{encoding}" : "text/plain"
        else
          content_type tree.mime_type
        end

        present tree.data
      end

N
Nihad Abbasov 已提交
303 304 305
    end
  end
end