projects.rb 8.5 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 43 44 45 46 47 48 49
        params[:code] ||= params[:name]
        params[:path] ||= params[:name]
        project_attrs = {}
        params.each_pair do |k ,v|
          if Project.attribute_names.include? k
            project_attrs[k] = v
          end
        end
        @project = Project.create_by_user(project_attrs, current_user)
50 51 52 53 54 55 56
        if @project.saved?
          present @project, with: Entities::Project
        else
          error!({'message' => '404 Not found'}, 404)
        end
      end

M
miks 已提交
57 58 59 60 61 62 63 64 65 66 67
      # 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

68 69 70 71 72 73 74
      # 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 已提交
75 76
      #   POST /projects/:id/users
      post ":id/users" do
M
miks 已提交
77
        user_project.add_users_ids_to_team(params[:user_ids].values, params[:project_access])
M
miks 已提交
78
        nil
79 80
      end

M
miks 已提交
81 82 83 84 85 86 87 88 89 90
      # 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
        user_project.update_users_ids_to_role(params[:user_ids].values, params[:project_access])
M
miks 已提交
91
        nil
M
miks 已提交
92 93 94 95 96 97 98 99 100 101 102
      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
        user_project.delete_users_ids_from_team(params[:user_ids].values)
M
miks 已提交
103
        nil
M
miks 已提交
104 105
      end

N
Nihad Abbasov 已提交
106 107 108
      # Get a project repository branches
      #
      # Parameters:
N
Nihad Abbasov 已提交
109
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
110 111 112
      # Example Request:
      #   GET /projects/:id/repository/branches
      get ":id/repository/branches" do
113
        present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject
N
Nihad Abbasov 已提交
114 115
      end

116 117 118 119
      # Get a single branch
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
120
      #   branch (required) - The name of the branch
121
      # Example Request:
122 123 124
      #   GET /projects/:id/repository/branches/:branch
      get ":id/repository/branches/:branch" do
        @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
125
        present @branch, with: Entities::RepoObject
126 127
      end

N
Nihad Abbasov 已提交
128 129 130
      # Get a project repository tags
      #
      # Parameters:
N
Nihad Abbasov 已提交
131
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
132 133 134
      # Example Request:
      #   GET /projects/:id/repository/tags
      get ":id/repository/tags" do
135
        present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject
N
Nihad Abbasov 已提交
136
      end
N
Nihad Abbasov 已提交
137 138 139 140

      # Get a project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
141
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
142 143 144 145
      #   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 已提交
146
        @snippet = user_project.snippets.find(params[:snippet_id])
147
        present @snippet, with: Entities::ProjectSnippet
N
Nihad Abbasov 已提交
148 149 150 151 152
      end

      # Create a new project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
153
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
154 155 156 157 158 159 160
      #   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
N
Nihad Abbasov 已提交
161
        @snippet = user_project.snippets.new(
162 163 164 165
          title: params[:title],
          file_name: params[:file_name],
          expires_at: params[:lifetime],
          content: params[:code]
N
Nihad Abbasov 已提交
166 167 168 169
        )
        @snippet.author = current_user

        if @snippet.save
170
          present @snippet, with: Entities::ProjectSnippet
N
Nihad Abbasov 已提交
171 172 173 174 175
        else
          error!({'message' => '404 Not found'}, 404)
        end
      end

176 177 178
      # Update an existing project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
179
      #   id (required) - The ID or code name of a project
180 181 182 183 184 185 186 187
      #   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 已提交
188
        @snippet = user_project.snippets.find(params[:snippet_id])
189
        parameters = {
190 191 192 193
          title: (params[:title] || @snippet.title),
          file_name: (params[:file_name] || @snippet.file_name),
          expires_at: (params[:lifetime] || @snippet.expires_at),
          content: (params[:code] || @snippet.content)
194 195 196
        }

        if @snippet.update_attributes(parameters)
197
          present @snippet, with: Entities::ProjectSnippet
198 199 200 201 202
        else
          error!({'message' => '404 Not found'}, 404)
        end
      end

N
Nihad Abbasov 已提交
203 204 205
      # Delete a project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
206
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
207 208 209 210
      #   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 已提交
211
        @snippet = user_project.snippets.find(params[:snippet_id])
N
Nihad Abbasov 已提交
212 213
        @snippet.destroy
      end
214 215 216 217

      # Get a raw project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
218
      #   id (required) - The ID or code name of a project
219 220 221 222
      #   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 已提交
223
        @snippet = user_project.snippets.find(params[:snippet_id])
224
        content_type 'text/plain'
225 226
        present @snippet.content
      end
227 228 229 230 231

      # Get a raw file contents
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
232
      #   sha (required) - The commit or branch name
233 234 235 236 237 238 239 240
      #   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
        error!('404 Commit Not Found', 404) unless commit
241

242 243
        tree = Tree.new commit.tree, user_project, ref, params[:filepath]
        error!('404 File Not Found', 404) unless tree.try(:tree)
244

245 246 247 248 249 250 251 252 253 254
        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 已提交
255 256 257
    end
  end
end