2012-08-17 14:40:57 +01:00
|
|
|
class Api::V1::ProjectsController < Api::V1::BaseController
|
2012-10-17 13:18:52 +01:00
|
|
|
|
2015-03-04 23:19:19 +00:00
|
|
|
before_action :authenticate_user!
|
|
|
|
skip_before_action :authenticate_user!, only: [:get_id, :show, :refs_list] if APP_CONFIG['anonymous_access']
|
2014-01-21 04:51:49 +00:00
|
|
|
|
2015-03-19 23:31:41 +00:00
|
|
|
before_action :load_project
|
2012-08-17 14:40:57 +01:00
|
|
|
|
2012-10-17 11:35:33 +01:00
|
|
|
def index
|
2015-03-19 23:31:41 +00:00
|
|
|
authorize :project
|
|
|
|
@projects = ProjectPolicy::Scope.new(current_user, Project).
|
|
|
|
membered.paginate(paginate_params)
|
2014-08-28 21:46:42 +01:00
|
|
|
respond_to :json
|
2012-10-17 11:35:33 +01:00
|
|
|
end
|
|
|
|
|
2012-08-28 17:57:12 +01:00
|
|
|
def get_id
|
2015-03-19 23:31:41 +00:00
|
|
|
authorize @project = Project.find_by_owner_and_name!(params[:owner], params[:name])
|
2014-08-28 21:46:42 +01:00
|
|
|
respond_to :json
|
2012-08-17 14:40:57 +01:00
|
|
|
end
|
2012-09-26 18:15:11 +01:00
|
|
|
|
|
|
|
def show
|
2014-08-28 21:46:42 +01:00
|
|
|
respond_to :json
|
2012-10-11 17:27:17 +01:00
|
|
|
end
|
2012-09-26 18:15:11 +01:00
|
|
|
|
2012-10-11 17:27:17 +01:00
|
|
|
def refs_list
|
2013-07-23 11:07:39 +01:00
|
|
|
@refs = @project.repo.branches + @project.repo.tags.select{ |t| t.commit }
|
2014-08-28 21:46:42 +01:00
|
|
|
respond_to :json
|
2012-09-26 18:15:11 +01:00
|
|
|
end
|
2012-10-11 17:27:17 +01:00
|
|
|
|
2012-10-17 13:18:52 +01:00
|
|
|
def update
|
|
|
|
update_subject @project
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
destroy_subject @project
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-03-19 23:31:41 +00:00
|
|
|
@project = Project.new(params[:project])
|
|
|
|
p_params = params[:project] || {}
|
|
|
|
owner_type = %w(User Group).find{ |t| t == p_params[:owner_type] }
|
|
|
|
if owner_type.present?
|
|
|
|
@project.owner = owner_type.constantize.find_by(id: p_params[:owner_id])
|
2012-10-17 13:18:52 +01:00
|
|
|
else
|
|
|
|
@project.owner = nil
|
|
|
|
end
|
2015-03-17 00:55:04 +00:00
|
|
|
authorize @project.owner, :write? if @project.owner != current_user
|
2012-10-17 13:18:52 +01:00
|
|
|
create_subject @project
|
|
|
|
end
|
|
|
|
|
|
|
|
def members
|
|
|
|
@members = @project.collaborators.order('uname').paginate(paginate_params)
|
2014-08-28 21:46:42 +01:00
|
|
|
respond_to :json
|
2012-10-17 13:18:52 +01:00
|
|
|
end
|
|
|
|
|
2012-10-17 14:46:16 +01:00
|
|
|
def add_member
|
|
|
|
add_member_to_subject @project, params[:role]
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_member
|
|
|
|
remove_member_from_subject @project
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_member
|
|
|
|
update_member_in_subject @project
|
|
|
|
end
|
|
|
|
|
2015-02-04 12:02:45 +00:00
|
|
|
def fork(is_alias = false)
|
2013-05-07 19:08:07 +01:00
|
|
|
owner = (Group.find params[:group_id] if params[:group_id].present?) || current_user
|
2015-03-17 00:55:04 +00:00
|
|
|
authorize owner, :write? if owner.class == Group
|
2015-02-04 12:02:45 +00:00
|
|
|
if forked = @project.fork(owner, new_name: params[:fork_name], is_alias: is_alias) and forked.valid?
|
2012-10-17 14:46:16 +01:00
|
|
|
render_json_response forked, 'Project has been forked successfully'
|
|
|
|
else
|
|
|
|
render_validation_error forked, 'Project has not been forked'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-04 12:02:45 +00:00
|
|
|
def alias
|
|
|
|
fork(true)
|
|
|
|
end
|
2015-03-19 23:31:41 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Private: before_action hook which loads Project.
|
|
|
|
def load_project
|
|
|
|
authorize @project = Project.find(params[:id]) if params[:id]
|
|
|
|
end
|
2012-08-17 14:40:57 +01:00
|
|
|
end
|