projects.rb 10.8 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
      # Get all projects for admin user
      #
      # Example Request:
      #   GET /projects/all
      get '/all' do
        authenticated_as_admin!
48 49
        @projects = Project.all
        @projects = filter_projects(@projects)
50
        @projects = paginate @projects
51 52 53
        present @projects, with: Entities::Project
      end

N
Nihad Abbasov 已提交
54 55 56
      # Get a single project
      #
      # Parameters:
57
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
58 59 60
      # Example Request:
      #   GET /projects/:id
      get ":id" do
61
        present user_project, with: Entities::ProjectWithAccess, user: current_user
N
Nihad Abbasov 已提交
62 63
      end

64
      # Get events for a single project
D
Dmitriy Zaporozhets 已提交
65 66 67 68
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
C
Ciro Santilli 已提交
69
      #   GET /projects/:id/events
D
Dmitriy Zaporozhets 已提交
70
      get ":id/events" do
71
        events = paginate user_project.events.recent
D
Dmitriy Zaporozhets 已提交
72 73 74
        present events, with: Entities::Event
      end

75 76 77 78
      # Create new project
      #
      # Parameters:
      #   name (required) - name for new project
79
      #   description (optional) - short project description
80 81
      #   issues_enabled (optional)
      #   merge_requests_enabled (optional)
82
      #   builds_enabled (optional)
83
      #   wiki_enabled (optional)
84
      #   snippets_enabled (optional)
85
      #   namespace_id (optional) - defaults to user namespace
86 87
      #   public (optional) - if true same as setting visibility_level = 20
      #   visibility_level (optional) - 0 by default
88
      #   import_url (optional)
89 90 91
      # Example Request
      #   POST /projects
      post do
92
        required_attributes! [:name]
93
        attrs = attributes_for_keys [:name,
94 95 96 97
                                     :path,
                                     :description,
                                     :issues_enabled,
                                     :merge_requests_enabled,
98
                                     :builds_enabled,
99 100 101
                                     :wiki_enabled,
                                     :snippets_enabled,
                                     :namespace_id,
102
                                     :public,
103 104
                                     :visibility_level,
                                     :import_url]
105
        attrs = map_public_to_visibility_level(attrs)
106
        @project = ::Projects::CreateService.new(current_user, attrs).execute
107 108 109
        if @project.saved?
          present @project, with: Entities::Project
        else
110 111 112
          if @project.errors[:limit_reached].present?
            error!(@project.errors[:limit_reached], 403)
          end
J
jubianchi 已提交
113
          render_validation_error!(@project)
114 115 116
        end
      end

A
Angus MacArthur 已提交
117 118 119 120 121 122 123
      # 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
124 125
      #   issues_enabled (optional)
      #   merge_requests_enabled (optional)
126
      #   builds_enabled (optional)
127 128
      #   wiki_enabled (optional)
      #   snippets_enabled (optional)
129 130
      #   public (optional) - if true same as setting visibility_level = 20
      #   visibility_level (optional)
131
      #   import_url (optional)
A
Angus MacArthur 已提交
132 133 134 135 136 137
      # 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,
138 139 140 141
                                     :description,
                                     :default_branch,
                                     :issues_enabled,
                                     :merge_requests_enabled,
142
                                     :builds_enabled,
143 144
                                     :wiki_enabled,
                                     :snippets_enabled,
145
                                     :public,
146 147
                                     :visibility_level,
                                     :import_url]
148
        attrs = map_public_to_visibility_level(attrs)
149
        @project = ::Projects::CreateService.new(user, attrs).execute
A
Angus MacArthur 已提交
150 151 152
        if @project.saved?
          present @project, with: Entities::Project
        else
J
jubianchi 已提交
153
          render_validation_error!(@project)
A
Angus MacArthur 已提交
154 155 156
        end
      end

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
      # 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

174 175 176 177 178 179 180 181 182
      # 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)
183
      #   builds_enabled (optional)
184 185 186 187 188 189 190 191 192 193 194 195 196
      #   wiki_enabled (optional)
      #   snippets_enabled (optional)
      #   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,
197
                                     :builds_enabled,
198 199 200 201 202 203 204 205 206 207 208 209 210 211
                                     :wiki_enabled,
                                     :snippets_enabled,
                                     :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

212
        if user_project.errors.any?
213
          render_validation_error!(user_project)
214 215
        else
          present user_project, with: Entities::Project
216 217 218
        end
      end

219 220 221 222 223 224 225 226
      # 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 已提交
227
        ::Projects::DestroyService.new(user_project, current_user, {}).execute
228
      end
A
Angus MacArthur 已提交
229

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
      # 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
247
          not_found!("Source Project")
248 249 250 251 252 253 254 255 256 257 258
        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
259
        authorize! :remove_fork_project, user_project
260
        if user_project.forked?
261 262 263
          user_project.forked_project_link.destroy
        end
      end
264 265 266 267
      # search for projects current_user has access to
      #
      # Parameters:
      #   query (required) - A string contained in the project name
268 269
      #   per_page (optional) - number of projects to return per page
      #   page (optional) - the page to retrieve
270 271 272 273
      # Example Request:
      #   GET /projects/search/:query
      get "/search/:query" do
        ids = current_user.authorized_projects.map(&:id)
274 275
        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]}%")
276
        sort = params[:sort] == 'desc' ? 'desc' : 'asc'
277 278

        projects = case params["order_by"]
279 280 281 282 283
                   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
284 285
                   end

286
        present paginate(projects), with: Entities::Project
287
      end
288 289 290 291 292 293 294 295 296 297


      # 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
298
        present @users, with: Entities::UserBasic
299
      end
N
Nihad Abbasov 已提交
300 301 302
    end
  end
end