projects.rb 11.5 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
      helpers do
8 9
        def map_public_to_visibility_level(attrs)
          publik = attrs.delete(:public)
10 11 12 13 14 15
          if publik.present? && !attrs[:visibility_level].present?
            publik = parse_boolean(publik)
            # Since setting the public attribute to private could mean either
            # private or internal, use the more conservative option, private.
            attrs[:visibility_level] = (publik == true) ? Gitlab::VisibilityLevel::PUBLIC : Gitlab::VisibilityLevel::PRIVATE
          end
16 17
          attrs
        end
18 19
      end

N
Nihad Abbasov 已提交
20 21 22 23 24
      # Get a projects list for authenticated user
      #
      # Example Request:
      #   GET /projects
      get do
D
Dmitriy Zaporozhets 已提交
25
        @projects = current_user.authorized_projects
26
        @projects = filter_projects(@projects)
D
Dmitriy Zaporozhets 已提交
27
        @projects = paginate @projects
28
        present @projects, with: Entities::Project
N
Nihad Abbasov 已提交
29 30
      end

31 32 33 34 35
      # Get an owned projects list for authenticated user
      #
      # Example Request:
      #   GET /projects/owned
      get '/owned' do
36
        @projects = current_user.owned_projects
37
        @projects = filter_projects(@projects)
38
        @projects = paginate @projects
39 40 41
        present @projects, with: Entities::Project
      end

42 43 44 45 46 47 48 49 50 51 52
      # Gets starred project for the authenticated user
      #
      # Example Request:
      #   GET /projects/starred
      get '/starred' do
        @projects = current_user.starred_projects
        @projects = filter_projects(@projects)
        @projects = paginate @projects
        present @projects, with: Entities::Project
      end

53 54 55 56 57 58
      # Get all projects for admin user
      #
      # Example Request:
      #   GET /projects/all
      get '/all' do
        authenticated_as_admin!
59 60
        @projects = Project.all
        @projects = filter_projects(@projects)
61
        @projects = paginate @projects
62 63 64
        present @projects, with: Entities::Project
      end

N
Nihad Abbasov 已提交
65 66 67
      # Get a single project
      #
      # Parameters:
68
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
69 70 71
      # Example Request:
      #   GET /projects/:id
      get ":id" do
72
        present user_project, with: Entities::ProjectWithAccess, user: current_user
N
Nihad Abbasov 已提交
73 74
      end

75
      # Get events for a single project
D
Dmitriy Zaporozhets 已提交
76 77 78 79
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
C
Ciro Santilli 已提交
80
      #   GET /projects/:id/events
D
Dmitriy Zaporozhets 已提交
81
      get ":id/events" do
82
        events = paginate user_project.events.recent
D
Dmitriy Zaporozhets 已提交
83 84 85
        present events, with: Entities::Event
      end

86 87 88 89
      # Create new project
      #
      # Parameters:
      #   name (required) - name for new project
90
      #   description (optional) - short project description
91 92
      #   issues_enabled (optional)
      #   merge_requests_enabled (optional)
93
      #   builds_enabled (optional)
94
      #   wiki_enabled (optional)
95
      #   snippets_enabled (optional)
K
Kamil Trzcinski 已提交
96
      #   shared_runners_enabled (optional)
97
      #   namespace_id (optional) - defaults to user namespace
98 99
      #   public (optional) - if true same as setting visibility_level = 20
      #   visibility_level (optional) - 0 by default
100
      #   import_url (optional)
101 102 103
      # Example Request
      #   POST /projects
      post do
104
        required_attributes! [:name]
105
        attrs = attributes_for_keys [:name,
106 107 108 109
                                     :path,
                                     :description,
                                     :issues_enabled,
                                     :merge_requests_enabled,
110
                                     :builds_enabled,
111 112
                                     :wiki_enabled,
                                     :snippets_enabled,
K
Kamil Trzcinski 已提交
113
                                     :shared_runners_enabled,
114
                                     :namespace_id,
115
                                     :public,
116 117
                                     :visibility_level,
                                     :import_url]
118
        attrs = map_public_to_visibility_level(attrs)
119
        @project = ::Projects::CreateService.new(current_user, attrs).execute
120 121 122
        if @project.saved?
          present @project, with: Entities::Project
        else
123 124 125
          if @project.errors[:limit_reached].present?
            error!(@project.errors[:limit_reached], 403)
          end
J
jubianchi 已提交
126
          render_validation_error!(@project)
127 128 129
        end
      end

A
Angus MacArthur 已提交
130 131 132 133 134 135 136
      # 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
137 138
      #   issues_enabled (optional)
      #   merge_requests_enabled (optional)
139
      #   builds_enabled (optional)
140 141
      #   wiki_enabled (optional)
      #   snippets_enabled (optional)
K
Kamil Trzcinski 已提交
142
      #   shared_runners_enabled (optional)
143 144
      #   public (optional) - if true same as setting visibility_level = 20
      #   visibility_level (optional)
145
      #   import_url (optional)
A
Angus MacArthur 已提交
146 147 148 149 150 151
      # 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,
152 153 154 155
                                     :description,
                                     :default_branch,
                                     :issues_enabled,
                                     :merge_requests_enabled,
156
                                     :builds_enabled,
157 158
                                     :wiki_enabled,
                                     :snippets_enabled,
K
Kamil Trzcinski 已提交
159
                                     :shared_runners_enabled,
160
                                     :public,
161 162
                                     :visibility_level,
                                     :import_url]
163
        attrs = map_public_to_visibility_level(attrs)
164
        @project = ::Projects::CreateService.new(user, attrs).execute
A
Angus MacArthur 已提交
165 166 167
        if @project.saved?
          present @project, with: Entities::Project
        else
J
jubianchi 已提交
168
          render_validation_error!(@project)
A
Angus MacArthur 已提交
169 170 171
        end
      end

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
      # Fork new project for the current user.
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request
      #   POST /projects/fork/:id
      post 'fork/:id' do
        @forked_project =
          ::Projects::ForkService.new(user_project,
                                      current_user).execute
        if @forked_project.errors.any?
          conflict!(@forked_project.errors.messages)
        else
          present @forked_project, with: Entities::Project
        end
      end

189 190 191 192 193 194 195 196 197
      # Update an existing project
      #
      # Parameters:
      #   id (required) - the id of a project
      #   name (optional) - name of a project
      #   path (optional) - path of a project
      #   description (optional) - short project description
      #   issues_enabled (optional)
      #   merge_requests_enabled (optional)
198
      #   builds_enabled (optional)
199 200
      #   wiki_enabled (optional)
      #   snippets_enabled (optional)
K
Kamil Trzcinski 已提交
201
      #   shared_runners_enabled (optional)
202 203 204 205 206 207 208 209 210 211 212
      #   public (optional) - if true same as setting visibility_level = 20
      #   visibility_level (optional) - visibility level of a project
      # Example Request
      #   PUT /projects/:id
      put ':id' do
        attrs = attributes_for_keys [:name,
                                     :path,
                                     :description,
                                     :default_branch,
                                     :issues_enabled,
                                     :merge_requests_enabled,
213
                                     :builds_enabled,
214 215
                                     :wiki_enabled,
                                     :snippets_enabled,
K
Kamil Trzcinski 已提交
216
                                     :shared_runners_enabled,
217 218 219 220 221 222 223 224 225 226 227 228
                                     :public,
                                     :visibility_level]
        attrs = map_public_to_visibility_level(attrs)
        authorize_admin_project
        authorize! :rename_project, user_project if attrs[:name].present?
        if attrs[:visibility_level].present?
          authorize! :change_visibility_level, user_project
        end

        ::Projects::UpdateService.new(user_project,
                                      current_user, attrs).execute

229
        if user_project.errors.any?
230
          render_validation_error!(user_project)
231 232
        else
          present user_project, with: Entities::Project
233 234 235
        end
      end

236 237 238 239 240 241 242 243
      # Remove project
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
      #   DELETE /projects/:id
      delete ":id" do
        authorize! :remove_project, user_project
V
Vinnie Okada 已提交
244
        ::Projects::DestroyService.new(user_project, current_user, {}).execute
245
      end
A
Angus MacArthur 已提交
246

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
      # Mark this project as forked from another
      #
      # Parameters:
      #   id: (required) - The ID of the project being marked as a fork
      #   forked_from_id: (required) - The ID of the project it was forked from
      # Example Request:
      #   POST /projects/:id/fork/:forked_from_id
      post ":id/fork/:forked_from_id" do
        authenticated_as_admin!
        forked_from_project = find_project(params[:forked_from_id])
        unless forked_from_project.nil?
          if user_project.forked_from_project.nil?
            user_project.create_forked_project_link(forked_to_project_id: user_project.id, forked_from_project_id: forked_from_project.id)
          else
            render_api_error!("Project already forked", 409)
          end
        else
264
          not_found!("Source Project")
265 266 267 268 269 270 271 272 273 274 275
        end

      end

      # Remove a forked_from relationship
      #
      # Parameters:
      # id: (required) - The ID of the project being marked as a fork
      # Example Request:
      #  DELETE /projects/:id/fork
      delete ":id/fork" do
276
        authorize! :remove_fork_project, user_project
277
        if user_project.forked?
278 279 280
          user_project.forked_project_link.destroy
        end
      end
281 282 283 284
      # search for projects current_user has access to
      #
      # Parameters:
      #   query (required) - A string contained in the project name
285 286
      #   per_page (optional) - number of projects to return per page
      #   page (optional) - the page to retrieve
287 288 289 290
      # Example Request:
      #   GET /projects/search/:query
      get "/search/:query" do
        ids = current_user.authorized_projects.map(&:id)
291 292
        visibility_levels = [ Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC ]
        projects = Project.where("(id in (?) OR visibility_level in (?)) AND (name LIKE (?))", ids, visibility_levels, "%#{params[:query]}%")
293
        sort = params[:sort] == 'desc' ? 'desc' : 'asc'
294 295

        projects = case params["order_by"]
296 297 298 299 300
                   when 'id' then projects.order("id #{sort}")
                   when 'name' then projects.order("name #{sort}")
                   when 'created_at' then projects.order("created_at #{sort}")
                   when 'last_activity_at' then projects.order("last_activity_at #{sort}")
                   else projects
301 302
                   end

303
        present paginate(projects), with: Entities::Project
304
      end
305 306 307 308 309 310 311 312 313 314


      # Get a users list
      #
      # Example Request:
      #  GET /users
      get ':id/users' do
        @users = User.where(id: user_project.team.users.map(&:id))
        @users = @users.search(params[:search]) if params[:search].present?
        @users = paginate @users
315
        present @users, with: Entities::UserBasic
316
      end
N
Nihad Abbasov 已提交
317 318 319
    end
  end
end