BigW Consortium Gitlab

projects.rb 11.4 KB
Newer Older
1
module API
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
          publik = parse_boolean(publik)
11 12 13
          attrs[:visibility_level] = Gitlab::VisibilityLevel::PUBLIC if !attrs[:visibility_level].present? && publik == true
          attrs
        end
14 15 16 17 18 19 20 21 22 23 24

        def filter_projects(projects)
          # If the archived parameter is passed, limit results accordingly
          if params[:archived].present?
            projects = projects.where(archived: parse_boolean(params[:archived]))
          end

          if params[:search].present?
            projects = projects.search(params[:search])
          end

25 26 27 28 29 30
          if params[:ci_enabled_first].present?
            projects.includes(:gitlab_ci_service).
              reorder("services.active DESC, projects.#{project_order_by} #{project_sort}")
          else
            projects.reorder(project_order_by => project_sort)
          end
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        end

        def project_order_by
          order_fields = %w(id name path created_at updated_at last_activity_at)

          if order_fields.include?(params['order_by'])
            params['order_by']
          else
            'created_at'
          end
        end

        def project_sort
          if params["sort"] == 'asc'
            :asc
          else
            :desc
          end
        end
50 51
      end

52 53 54 55 56
      # Get a projects list for authenticated user
      #
      # Example Request:
      #   GET /projects
      get do
57
        @projects = current_user.authorized_projects
58
        @projects = filter_projects(@projects)
59
        @projects = paginate @projects
60
        present @projects, with: Entities::Project
61 62
      end

63 64 65 66 67
      # Get an owned projects list for authenticated user
      #
      # Example Request:
      #   GET /projects/owned
      get '/owned' do
68
        @projects = current_user.owned_projects
69
        @projects = filter_projects(@projects)
70
        @projects = paginate @projects
71 72 73
        present @projects, with: Entities::Project
      end

74 75 76 77 78 79
      # Get all projects for admin user
      #
      # Example Request:
      #   GET /projects/all
      get '/all' do
        authenticated_as_admin!
80 81
        @projects = Project.all
        @projects = filter_projects(@projects)
82
        @projects = paginate @projects
83 84 85
        present @projects, with: Entities::Project
      end

86 87 88
      # Get a single project
      #
      # Parameters:
89
      #   id (required) - The ID of a project
90 91 92
      # Example Request:
      #   GET /projects/:id
      get ":id" do
93
        present user_project, with: Entities::ProjectWithAccess, user: current_user
94 95
      end

96
      # Get events for a single project
97 98 99 100
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
101
      #   GET /projects/:id/events
102
      get ":id/events" do
103
        events = paginate user_project.events.recent
104 105 106
        present events, with: Entities::Event
      end

107 108 109 110
      # Create new project
      #
      # Parameters:
      #   name (required) - name for new project
111
      #   description (optional) - short project description
112 113 114
      #   issues_enabled (optional)
      #   merge_requests_enabled (optional)
      #   wiki_enabled (optional)
115
      #   snippets_enabled (optional)
116
      #   namespace_id (optional) - defaults to user namespace
117 118
      #   public (optional) - if true same as setting visibility_level = 20
      #   visibility_level (optional) - 0 by default
119
      #   import_url (optional)
120 121 122
      # Example Request
      #   POST /projects
      post do
123
        required_attributes! [:name]
124
        attrs = attributes_for_keys [:name,
125 126 127 128 129 130 131
                                     :path,
                                     :description,
                                     :issues_enabled,
                                     :merge_requests_enabled,
                                     :wiki_enabled,
                                     :snippets_enabled,
                                     :namespace_id,
132
                                     :public,
133 134
                                     :visibility_level,
                                     :import_url]
135
        attrs = map_public_to_visibility_level(attrs)
136
        @project = ::Projects::CreateService.new(current_user, attrs).execute
137 138 139
        if @project.saved?
          present @project, with: Entities::Project
        else
140 141 142
          if @project.errors[:limit_reached].present?
            error!(@project.errors[:limit_reached], 403)
          end
143
          render_validation_error!(@project)
144 145 146
        end
      end

Angus MacArthur committed
147 148 149 150 151 152 153
      # 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
154 155
      #   issues_enabled (optional)
      #   merge_requests_enabled (optional)
156 157
      #   wiki_enabled (optional)
      #   snippets_enabled (optional)
158 159
      #   public (optional) - if true same as setting visibility_level = 20
      #   visibility_level (optional)
160
      #   import_url (optional)
Angus MacArthur committed
161 162 163 164 165 166
      # 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,
167 168 169 170 171 172
                                     :description,
                                     :default_branch,
                                     :issues_enabled,
                                     :merge_requests_enabled,
                                     :wiki_enabled,
                                     :snippets_enabled,
173
                                     :public,
174 175
                                     :visibility_level,
                                     :import_url]
176
        attrs = map_public_to_visibility_level(attrs)
177
        @project = ::Projects::CreateService.new(user, attrs).execute
Angus MacArthur committed
178 179 180
        if @project.saved?
          present @project, with: Entities::Project
        else
181
          render_validation_error!(@project)
Angus MacArthur committed
182 183 184
        end
      end

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
      # 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

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
      # 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)
      #   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,
                                     :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

238
        if user_project.errors.any?
239
          render_validation_error!(user_project)
240 241
        else
          present user_project, with: Entities::Project
242 243 244
        end
      end

245 246 247 248 249 250 251 252
      # Remove project
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
      #   DELETE /projects/:id
      delete ":id" do
        authorize! :remove_project, user_project
Vinnie Okada committed
253
        ::Projects::DestroyService.new(user_project, current_user, {}).execute
254
      end
Angus MacArthur committed
255

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
      # 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
273
          not_found!("Source Project")
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
        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
        authenticated_as_admin!
        unless user_project.forked_project_link.nil?
          user_project.forked_project_link.destroy
        end
      end
290 291 292 293
      # search for projects current_user has access to
      #
      # Parameters:
      #   query (required) - A string contained in the project name
294 295
      #   per_page (optional) - number of projects to return per page
      #   page (optional) - the page to retrieve
296 297 298 299
      # Example Request:
      #   GET /projects/search/:query
      get "/search/:query" do
        ids = current_user.authorized_projects.map(&:id)
300 301
        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]}%")
302
        sort = params[:sort] == 'desc' ? 'desc' : 'asc'
303 304

        projects = case params["order_by"]
305 306 307 308 309
                   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
310 311
                   end

312
        present paginate(projects), with: Entities::Project
313
      end
314 315 316 317 318 319 320 321 322 323


      # 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
324
        present @users, with: Entities::UserBasic
325
      end
326 327 328
    end
  end
end