projects.rb 7.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
          publik = parse_boolean(publik)
11 12 13
          attrs[:visibility_level] = Gitlab::VisibilityLevel::PUBLIC if !attrs[:visibility_level].present? && publik == true
          attrs
        end
14 15
      end

N
Nihad Abbasov 已提交
16 17
      # Get a projects list for authenticated user
      #
18 19 20
      # Parameters:
      #   archived (optional) - if passed, limit by archived status
      #
N
Nihad Abbasov 已提交
21 22 23
      # Example Request:
      #   GET /projects
      get do
D
Dmitriy Zaporozhets 已提交
24 25
        @projects = current_user.authorized_projects

26 27
        # If the archived parameter is passed, limit results accordingly
        if params[:archived].present?
D
Dmitriy Zaporozhets 已提交
28
          @projects = @projects.where(archived: parse_boolean(params[:archived]))
29
        end
D
Dmitriy Zaporozhets 已提交
30 31

        @projects = paginate @projects
32
        present @projects, with: Entities::Project
N
Nihad Abbasov 已提交
33 34
      end

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

44 45 46 47 48 49 50 51 52 53
      # Get all projects for admin user
      #
      # Example Request:
      #   GET /projects/all
      get '/all' do
        authenticated_as_admin!
        @projects = paginate Project
        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

D
Dmitriy Zaporozhets 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77
      # Get a single project events
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
      #   GET /projects/:id
      get ":id/events" do
        limit = (params[:per_page] || 20).to_i
        offset = (params[:page] || 0).to_i * limit
        events = user_project.events.recent.limit(limit).offset(offset)

        present events, with: Entities::Event
      end

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

A
Angus MacArthur 已提交
118 119 120 121 122 123 124
      # 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
125 126
      #   issues_enabled (optional)
      #   merge_requests_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 142 143
                                     :description,
                                     :default_branch,
                                     :issues_enabled,
                                     :merge_requests_enabled,
                                     :wiki_enabled,
                                     :snippets_enabled,
144
                                     :public,
145 146
                                     :visibility_level,
                                     :import_url]
147
        attrs = map_public_to_visibility_level(attrs)
148
        @project = ::Projects::CreateService.new(user, attrs).execute
A
Angus MacArthur 已提交
149 150 151
        if @project.saved?
          present @project, with: Entities::Project
        else
J
jubianchi 已提交
152
          render_validation_error!(@project)
A
Angus MacArthur 已提交
153 154 155
        end
      end

156 157 158 159 160 161 162 163 164 165
      # Remove project
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
      #   DELETE /projects/:id
      delete ":id" do
        authorize! :remove_project, user_project
        user_project.destroy
      end
A
Angus MacArthur 已提交
166

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
      # 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
          not_found!
        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
201 202 203 204
      # search for projects current_user has access to
      #
      # Parameters:
      #   query (required) - A string contained in the project name
205 206
      #   per_page (optional) - number of projects to return per page
      #   page (optional) - the page to retrieve
207 208 209 210
      # Example Request:
      #   GET /projects/search/:query
      get "/search/:query" do
        ids = current_user.authorized_projects.map(&:id)
211 212
        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]}%")
213
        present paginate(projects), with: Entities::Project
214
      end
215 216 217 218 219 220 221 222 223 224


      # 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
225
        present @users, with: Entities::UserBasic
226
      end
N
Nihad Abbasov 已提交
227 228 229
    end
  end
end