BigW Consortium Gitlab

projects.rb 7.39 KB
Newer Older
1
module API
2 3 4 5 6
  # Projects API
  class Projects < Grape::API
    before { authenticate! }

    resource :projects do
7 8 9 10 11 12 13 14 15
      helpers do
        def handle_project_member_errors(errors)
          if errors[:project_access].any?
            error!(errors[:project_access], 422)
          end
          not_found!
        end
      end

16 17 18 19 20
      # Get a projects list for authenticated user
      #
      # Example Request:
      #   GET /projects
      get do
21
        @projects = paginate current_user.authorized_projects
22
        present @projects, with: Entities::Project
23 24
      end

25 26 27 28 29 30 31 32 33
      # 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

34 35 36
      # Get a single project
      #
      # Parameters:
37
      #   id (required) - The ID of a project
38 39 40
      # Example Request:
      #   GET /projects/:id
      get ":id" do
41
        present user_project, with: Entities::Project
42 43
      end

44 45 46 47 48 49 50 51 52 53 54 55 56 57
      # 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

58 59 60 61
      # Create new project
      #
      # Parameters:
      #   name (required) - name for new project
62 63
      #   description (optional) - short project description
      #   default_branch (optional) - 'master' by default
64 65 66 67 68
      #   issues_enabled (optional) 
      #   wall_enabled (optional) 
      #   merge_requests_enabled (optional) 
      #   wiki_enabled (optional) 
      #   snippets_enabled (optional)
69
      #   namespace_id (optional) - defaults to user namespace
70 71 72
      # Example Request
      #   POST /projects
      post do
73
        required_attributes! [:name]
74
        attrs = attributes_for_keys [:name,
75 76 77 78 79
                                    :description,
                                    :default_branch,
                                    :issues_enabled,
                                    :wall_enabled,
                                    :merge_requests_enabled,
80
                                    :wiki_enabled,
81
                                    :snippets_enabled,
82
                                    :namespace_id]
83
        @project = ::Projects::CreateContext.new(current_user, attrs).execute
84 85 86
        if @project.saved?
          present @project, with: Entities::Project
        else
87 88 89
          if @project.errors[:limit_reached].present?
            error!(@project.errors[:limit_reached], 403)
          end
90
          not_found!
91 92 93
        end
      end

Angus MacArthur committed
94 95 96 97 98 99 100
      # 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
101 102 103 104 105
      #   issues_enabled (optional) 
      #   wall_enabled (optional) 
      #   merge_requests_enabled (optional) 
      #   wiki_enabled (optional)
      #   snippets_enabled (optional)
Angus MacArthur committed
106 107 108 109 110 111 112 113 114 115 116
      # 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,
                                    :description,
                                    :default_branch,
                                    :issues_enabled,
                                    :wall_enabled,
                                    :merge_requests_enabled,
117 118
                                    :wiki_enabled,
                                    :snippets_enabled]
Angus MacArthur committed
119 120 121 122 123 124 125 126 127
        @project = ::Projects::CreateContext.new(user, attrs).execute
        if @project.saved?
          present @project, with: Entities::Project
        else
          not_found!
        end
      end


128
      # Get a project team members
129 130
      #
      # Parameters:
131
      #   id (required) - The ID of a project
132
      #   query         - Query string
133
      # Example Request:
134 135
      #   GET /projects/:id/members
      get ":id/members" do
136 137 138 139 140
        if params[:query].present?
          @members = paginate user_project.users.where("username LIKE ?", "%#{params[:query]}%")
        else
          @members = paginate user_project.users
        end
141
        present @members, with: Entities::ProjectMember, project: user_project
142 143
      end

144
      # Get a project team members
145 146
      #
      # Parameters:
147
      #   id (required) - The ID of a project
148
      #   user_id (required) - The ID of a user
149
      # Example Request:
150 151 152 153 154 155 156 157 158
      #   GET /projects/:id/members/:user_id
      get ":id/members/:user_id" do
        @member = user_project.users.find params[:user_id]
        present @member, with: Entities::ProjectMember, project: user_project
      end

      # Add a new project team member
      #
      # Parameters:
159
      #   id (required) - The ID of a project
160 161 162 163 164
      #   user_id (required) - The ID of a user
      #   access_level (required) - Project access level
      # Example Request:
      #   POST /projects/:id/members
      post ":id/members" do
randx committed
165
        authorize! :admin_project, user_project
166
        required_attributes! [:user_id, :access_level]
167 168 169 170 171 172 173 174 175 176 177 178

        # either the user is already a team member or a new one
        team_member = user_project.team_member_by_id(params[:user_id])
        if team_member.nil?
          team_member = user_project.users_projects.new(
            user_id: params[:user_id],
            project_access: params[:access_level]
          )
        end

        if team_member.save
          @member = team_member.user
179 180
          present @member, with: Entities::ProjectMember, project: user_project
        else
181
          handle_project_member_errors team_member.errors
182
        end
183 184
      end

185
      # Update project team member
186 187
      #
      # Parameters:
188
      #   id (required) - The ID of a project
189 190
      #   user_id (required) - The ID of a team member
      #   access_level (required) - Project access level
191
      # Example Request:
192 193
      #   PUT /projects/:id/members/:user_id
      put ":id/members/:user_id" do
randx committed
194
        authorize! :admin_project, user_project
195
        required_attributes! [:access_level]
196

197
        team_member = user_project.users_projects.find_by_user_id(params[:user_id])
198
        not_found!("User can not be found") if team_member.nil?
199 200 201

        if team_member.update_attributes(project_access: params[:access_level])
          @member = team_member.user
202 203
          present @member, with: Entities::ProjectMember, project: user_project
        else
204
          handle_project_member_errors team_member.errors
205
        end
206 207
      end

208
      # Remove a team member from project
209 210
      #
      # Parameters:
211
      #   id (required) - The ID of a project
212
      #   user_id (required) - The ID of a team member
213
      # Example Request:
214 215
      #   DELETE /projects/:id/members/:user_id
      delete ":id/members/:user_id" do
randx committed
216
        authorize! :admin_project, user_project
217 218 219
        team_member = user_project.users_projects.find_by_user_id(params[:user_id])
        unless team_member.nil?
          team_member.destroy
220
        else
221
          {message: "Access revoked", id: params[:user_id].to_i}
222
        end
223
      end
224 225 226
    end
  end
end